【发布时间】:2017-01-23 21:53:22
【问题描述】:
我正在尝试使用 Pyside 呈现网页的 JavaScript 生成的 HTML,然后使用该 html 进行网页抓取。我开始使用this quick example,但结果非常不一致。
问题是有些页面可以正常工作,但有些页面会无限挂起。而且我不是说几秒钟后就放弃,我让我的脚本在不同的时间运行了几个小时,但没有取得任何进展。
我目前的代码如下:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished[bool].connect(self.end)
self.mainFrame().load(url)
self.app.exec_()
def end(self, result):
print 'end'
self.finalFrame = self.mainFrame()
self.app.quit()
r = Render('http://pyside.github.io/docs/pyside/PySide/QtWebKit/index.html')
print r.finalFrame.toHtml().encode('ascii', 'ignore')
print 'done'
这个页面和this answer 中给出的页面一样有效,但大多数其他页面('https://www.google.ca/'、'https://webscraping.com')不能。
如何让这些页面加载?
【问题讨论】:
-
问题必须在你的最后,因为我加载任何这些网页都没有问题。所以这实际上只是你关于这个主题的其他问题的重复,除非你有一些重要的新信息要添加。尝试过使用不同的方法加载这些网址,例如urllib2?
-
是的,我可以使用 urllib2 加载它们并且它可以正常工作。我提到的网址要么从不加载,要么花费的时间超过 6 小时(到目前为止)。从我正在阅读的内容来看,这可能是 SSL 错误,但由于各种原因(我没有使用套接字、无法导入 QSslConfiguration 模块等),我发现的所有建议修复都无法实施。跨度>
-
您能否说明您正在使用哪些特定版本的 PySide 和 Qt4,以及在什么平台上?另外,请确保您在标准控制台中测试代码,而不是在 IDE 或调试器中。
标签: javascript python python-2.7 webkit pyside