如何在Python线程中使用qtwebkit?
作者:互联网
我试图用qtwebkit解析js生成的网页,我找到了一个如何获取页面源代码的示例:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtWebKit import *
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()
url = 'http://www.thesite.gov/search'
r = Render(url)
html = r.frame.toHtml()
但是我不知道如何使它在线程中工作.
那么,如何做到这一点,如果不可能的话-还有另一种快速的方法来获取js生成的网页吗?
解决方法:
考虑到QT的异步特性,QtWebkit方法也是非阻塞的,因此在线程中运行它们是没有意义的.您可以像这样并行启动它们:
from functools import partial
from PySide.QtCore import QUrl
from PySide.QtGui import QApplication
from PySide.QtWebKit import QWebView, QWebSettings
TARGET_URLS = (
'https://stackoverflow.com',
'http://github.com',
'http://bitbucket.org',
'http://news.ycombinator.com',
'http://slashdot.org',
'http://www.reddit.com',
'http://www.dzone.com',
'http://www.ideone.com',
'http://jsfiddle.net',
)
class Crawler(object):
def __init__(self, app):
self.app = app
self.results = dict()
self.browsers = dict()
def _load_finished(self, browser_id, ok):
print ok, browser_id
web_view, _flag = self.browsers[browser_id]
self.browsers[browser_id] = (web_view, True)
frame = web_view.page().mainFrame()
self.results[frame.url()] = frame.toHtml()
web_view.loadFinished.disconnect()
web_view.stop()
if all([closed for bid, closed in self.browsers.values()]):
print 'all finished'
self.app.quit()
def start(self, urls):
for browser_id, url in enumerate(urls):
web_view = QWebView()
web_view.settings().setAttribute(QWebSettings.AutoLoadImages,
False)
loaded = partial(self._load_finished, browser_id)
web_view.loadFinished.connect(loaded)
web_view.load(QUrl(url))
self.browsers[browser_id] = (web_view, False)
if __name__ == '__main__':
app = QApplication([])
crawler = Crawler(app)
crawler.start(TARGET_URLS)
app.exec_()
print 'got:', crawler.results.keys()
标签:qtwebkit,python,multithreading 来源: https://codeday.me/bug/20191011/1892516.html