【发布时间】: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 &url, NavigationType type, bool isMainFrame). -
我本可以猜到你在说什么,但作为一个不是专家的人,尤其是 Python 领域的专家,如果我不知道为什么某些东西不起作用,那将无济于事知道如何修复它----使用代码示例。
-
@LesPaul。
QtWebEngine不是QWebKit的直接替代品 - 许多功能已更改或完全缺失。
标签: python python-3.x pyqt pyqt4 pyqt5