我用过 PyQt4.QtWebkit 在我的 django 应用程序中抓取网页。在生产环境中,该模块无法抓取它。它会抛出错误“manage.py:无法连接到 X 服务器”
我的 Qt 类(class):
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
从 django-shell 调用:
r = Render(url)
当我使用 Django-shell(python manage.py shell)通过 django 调用这个“渲染”类时,渲染函数会引发错误。
你能帮我解决这个问题吗?
最佳答案
原因是"Xvfb"
我需要使用 xvfb(X 虚拟帧缓冲区)在 bash shell 中运行我的 python 程序
同样地,
ubuntu@localhost$ xvfb-run python webpage_scrapper.py http://www.google.ca/search?q=navaspot
它给出了结果。
现在我的要求是我需要在 python 中执行这个 shell 命令并等待 tine 收集结果。我必须处理结果。
您能否建议我在 python 上有效地执行此命令。
关于django - 管理.py : cannot connect to X server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8595538/