无法在同一应用程序中使用 QWebPage 两次
我正在尝试创建一个小型网络服务器,使用 webkit 加载 URL 以从网页中提取一些数据(例如:标题、图像大小...)。
我正在使用 PyQt4 从 python 访问 webkit。对于每个请求,我创建一个 QThread: - 创建一个 QWebPage 对象, - 运行事件循环 - 当网页加载完成(loadFinished信号)时,一些代码从QWebPage的mainFrame中提取数据并杀死QThread
这第一次工作得很好,网页被加载,包括其所有资源(CSS,图像) )。 第二次我要求服务器加载 url,网页被加载,但没有加载任何资源(没有 css,没有图像)。因此,当我尝试检索图像大小时,所有大小都设置为 0,0。
以下是一些代码片段:
# The QThread responsible of loading the WebPage class WebKitThread(QThread): def __init__(self, url): QThread.__init__(self) self.url = url self.start() def run(self): self.webkitParser = WebKitParser(self.url) self.exec_() class WebKitParser(QWebPage): def __init__(self, url, parent=None): QWebPage.__init__(self, parent ) self.loadFinished.connect(self._loadFinished) self.mainFrame().load(QUrl(url)) def _loadFinished(self, result): self.computePageProperties() QThread.currentThread().exit() def computePageProperties(self): # Some custom code that reads title, image size... self.computedTitle=XXXXXXXX
调用代码(响应 HTTP 请求)正在执行:
t = WebKitThread(url) t.wait() # do some stuff with properties of WebKitParser print t.webkitParser.computedTitle
I'm trying to create a little web server that loads, using webkit, an URL to extract some data from the web page (eg: title, images sizes...).
I'm using PyQt4 to access from python to webkit. For each request, I'm creating a QThread that:
- creates an QWebPage object,
- run an event loop
- when the loading of the webpage has finished (loadFinished signal), some code extracts data from the mainFrame of the QWebPage and kills the QThread
This works very well the first time, the web page is loaded, included all its resources (CSS, images).
The second time I ask the server to load an url, the web page is loaded, but none of its resources (no css, no images). So when I try to retrieve image sizes, all size are set to 0,0.
Here is some code snipset:
# The QThread responsible of loading the WebPage class WebKitThread(QThread): def __init__(self, url): QThread.__init__(self) self.url = url self.start() def run(self): self.webkitParser = WebKitParser(self.url) self.exec_() class WebKitParser(QWebPage): def __init__(self, url, parent=None): QWebPage.__init__(self, parent ) self.loadFinished.connect(self._loadFinished) self.mainFrame().load(QUrl(url)) def _loadFinished(self, result): self.computePageProperties() QThread.currentThread().exit() def computePageProperties(self): # Some custom code that reads title, image size... self.computedTitle=XXXXXXXX
The calling code (that respond to the HTTP request) is executing:
t = WebKitThread(url) t.wait() # do some stuff with properties of WebKitParser print t.webkitParser.computedTitle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经设法解决了这个问题:在 GUI 线程(QApplication 事件循环的线程)中创建 QWebPage 可以解决这个问题。
似乎第二次使用 QWebPage 时,它会尝试访问浏览器缓存(即使它已被配置禁用)。但是,如果第一个 QWebPage 不是在主 GUI 线程中创建的,则缓存配置有些错误并且不可用。
为了在主 GUI 线程中创建 QWebPage,我使用自定义 QEvent(User 类型的 QEvent)来触发 QWebPage 初始化和结果获取。
I've managed to fix the issue: creating the QWebPage in the GUI thread (the thread of QApplication event loop) fixes the issue.
It seems the second time a QWebPage is used, it tries to access to the browser cache (even if it has been disabled by configuration). But if the first QWebPage was not created in the main GUI thread, the cache is somewhat misconfigured and not usable.
To create the QWebPage in the main GUI thread I'm using a custom QEvent (QEvent of type User) that triggers QWebPage initialization and result fetching.