【问题标题】:PyQt4 to PyQt5 -> mainFrame() deprecated, need fix to load web pagesPyQt4 到 PyQt5 -> mainFrame() 已弃用,需要修复才能加载网页
【发布时间】:2017-06-28 02:32:53
【问题描述】:

我正在学习 Sentdex 的 PyQt4 YouTube 教程right here。我正在尝试跟进,但改用 PyQt5。这是一个简单的网络抓取应用程序。我跟着 Sentdex 的教程,我到了这里:

现在我正在尝试使用 PyQt5 编写相同的应用程序,这就是我所拥有的:

import os
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from bs4 import BeautifulSoup
import requests


class Client(QWebEnginePage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.load(QUrl(url))
        self.app.exec_()

    def _loadFinished(self):
        self.app.quit()


url = 'https://pythonprogramming.net/parsememcparseface/'
client_response = Client(url)

#I think the issue is here at LINE 26
source = client_response.mainFrame().toHtml()

soup = BeautifulSoup(source, "html.parser")
js_test = soup.find('p', class_='jstest')
print(js_test.text)

当我运行它时,我收到消息:

source = client_response.mainFrame().toHtml()
AttributeError: 'Client' object has no attribute 'mainFrame'

我尝试了几种不同的解决方案,但都没有奏效。任何帮助将不胜感激。

编辑

在第 15 行记录 QUrl(url) 返回此值:

PyQt5.QtCore.QUrl('https://pythonprogramming.net/parsememcparseface/')

当我在第 26 行尝试 source = client_response.load(QUrl(url)) 时,我最终得到以下消息:

File "test3.py", line 28, in <module> soup = BeautifulSoup(source, "html.parser") File "/Users/MYNAME/.venv/qtproject/lib/python3.6/site-packages/bs4/__init__.py", line 192, in __init__ elif len(markup) <= 256 and ( TypeError: object of type 'NoneType' has no len()

当我尝试source = client_response.url() 时,我得到:

soup = BeautifulSoup(source, "html.parser")
      File "/Users/MYNAME/.venv/qtproject/lib/python3.6/site-packages/bs4/__init__.py", line 192, in __init__
        elif len(markup) <= 256 and (
    TypeError: object of type 'QUrl' has no len()

【问题讨论】:

  • 看起来 mainFrame() 是class Client(QWebEnginePage) 中的自定义方法,因为根据Qt5 Documentation 在类中不存在它。您确定没有更多您缺少的教程吗?
  • mainFrame() 是 PyQt4 中带有 QWebPage 的方法:doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html
  • 在对 webkit 了解不多的情况下,似乎 mainFrame() 已被其他功能所吸收,而您可以使用布尔指示符指定框架是主框架还是子框架。例如acceptNavigationRequest(const QUrl &amp;url, NavigationType type, bool isMainFrame).
  • 我本可以猜到你在说什么,但作为一个不是专家的人,尤其是 Python 领域的专家,如果我不知道为什么某些东西不起作用,那将无济于事知道如何修复它----使用代码示例。
  • @LesPaul。 QtWebEngine 不是 QWebKit 的直接替代品 - 许多功能已更改或完全缺失。

标签: python python-3.x pyqt pyqt4 pyqt5


【解决方案1】:

你必须在类的定义中调用QWebEnginePage::toHtml()QWebEnginePage::toHtml() 接受一个指针函数或一个 lambda 作为参数,而这个指针函数又必须接受一个 'str' 类型的参数(这是包含页面 html 的参数)。下面是示例代码。

import bs4 as bs
import sys
import urllib.request
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl

class Page(QWebEnginePage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ''
        self.loadFinished.connect(self._on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def _on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print('Load finished')

    def Callable(self, html_str):
        self.html = html_str
        self.app.quit()


def main():
    page = Page('https://pythonprogramming.net/parsememcparseface/')
    soup = bs.BeautifulSoup(page.html, 'html.parser')
    js_test = soup.find('p', class_='jstest')
    print js_test.text

if __name__ == '__main__': main()

【讨论】:

  • 如果我只需要获取一页,这可以正常工作。但是,如果我创建一个循环,在其中下载页面的每个循环循环 python 崩溃。知道该怎么做吗?没有任何异常或错误消息 - Python 本身崩溃并且 OSX 提供提交错误报告
  • @KaizerSozay 我也有同样的问题,你找到解决办法了吗?
  • 不知道。不记得我做了什么
  • html_str如何包含html数据?
【解决方案2】:

永远不会太晚... 我遇到了同样的问题,在这里找到了它的描述:http://pyqt.sourceforge.net/Docs/PyQt5/gotchas.html#crashes-on-exit

我遵循将 QApplication 放在全局变量中的建议(我知道它很脏......我会因此受到惩罚)并且它工作“很好”。我可以循环而不会崩溃。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-05-07
    • 2021-11-06
    • 2021-11-08
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多