【问题标题】:Using Python's QtWebkit to render Javascript-based page, get QThread: Destroyed while thread is still running使用 Python 的 QtWebkit 渲染基于 Javascript 的页面,得到 QThread: Destroyed while thread is still running
【发布时间】:2016-11-11 05:57:08
【问题描述】:

我正在尝试为通过 JavaScript 加载的网页编写 Beautifulsoup 爬虫,Beautifulsoup 无法解析该网页。为了解决这个问题,我按照this tutorial 使用 QtWebkit 渲染页面,然后使用 Beautifulsoup 从生成的 HTML 中提取页面中的所有 href。

但是,页面抓取非常大,在完成获取这些链接之前,它会抛出错误“QThread: Destroyed while thread is still running”。许多人已经发布了有关此错误的问题并收到了答案,但是这些都是针对以 PyQT 为应用程序核心的更复杂的项目,因此这些回复假定您熟悉该库并且我在尝试应用它们时遇到了真正的麻烦我的情况。

似乎我需要通过将线程保存在变量中来防止线程被垃圾收集,但正确的方法却让我望而却步。

这是我的代码:

import sys
from PyQt4.QtGui import *  
from PyQt4.QtCore import *  
from PyQt4.QtWebKit import *  
from bs4 import BeautifulSoup

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.lolesports.com/en_US/msi/msi_2016/schedule/default'  
r = Render(url)  
result = r.frame.toHtml()

soup = BeautifulSoup(result, "html.parser")
for link in soup.find_all('a'):
    print(link.get('href'))

【问题讨论】:

  • QApplication.exec_() 阻塞,直到应用程序退出/中止。不要把它放在init中。

标签: python multithreading pyqt beautifulsoup web-crawler


【解决方案1】:

脚本没有明显错误,除了一些无害的 Qt 警告消息外,它似乎或多或少地按预期工作。

您收到的 Qt 消息并不能真正诊断任何东西,也不一定表示致命错误情况。所以我怀疑脚本实际上对你来说是正确的,你可能只是误解了输出。

如果您想删除所有 Qt 消息,请将以下行放在脚本顶部(就在导入下方):

qInstallMsgHandler(lambda *args: None)

PS:这是我得到的确切输出:

QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)
QNetworkReplyImplPrivate::error: Internal problem, this method must only be called once.
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)
http://na.leagueoflegends.com/en/
http://na.leagueoflegends.com/en/
http://na.leagueoflegends.com/en/news/
http://gameinfo.na.leagueoflegends.com/en/game-info/
http://nexus.leagueoflegends.com/
http://www.lolesports.com/en_US
http://boards.na.leagueoflegends.com/en/
http://ulol.leagueoflegends.com/
https://support.riotgames.com/hc/en-us
https://na.merch.riotgames.com/en/
http://na.leagueoflegends.com/en/news/
http://gameinfo.na.leagueoflegends.com/en/game-info/
http://nexus.leagueoflegends.com/
http://www.lolesports.com/en_US
http://boards.na.leagueoflegends.com/en/
http://ulol.leagueoflegends.com/
https://support.riotgames.com/hc/en-us
https://na.merch.riotgames.com/en/
http://signup.na.leagueoflegends.com/en
None
http://www.lolesports.com/en_US
http://eu.lolesports.com/en
http://eu.lolesports.com/pl
http://eu.lolesports.com/en
http://eu.lolesports.com/fr
http://eu.lolesports.com/es
http://eu.lolesports.com/de
http://lan.lolesports.com/
http://las.lolesports.com/
http://lolesports.com.br/
http://www.lolespor.com/
http://oce.lolesports.com/
javascript:;
javascript:;
javascript:;
javascript:;

【讨论】:

  • 感谢您的回答,很高兴知道这不是问题(不幸的是,我没有足够的声誉来实际支持您,哈哈)
猜你喜欢
  • 1970-01-01
  • 2021-03-20
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
相关资源
最近更新 更多