問題描述
我正在學習 Sentdex 的 PyQt4 YouTube 教程
現在我正在嘗試使用 PyQt5 編寫相同的應用程序,這就是我所擁有的:
導入操作系統導入系統從 PyQt5.QtWidgets 導入 QApplication從 PyQt5.QtCore 導入 QUrl,QEventLoop從 PyQt5.QtWebEngineWidgets 導入 QWebEnginePage從 bs4 導入 BeautifulSoup導入請求類客戶端(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 = 客戶端(網址)#我認為問題出在第 26 行源 = client_response.mainFrame().toHtml()湯= BeautifulSoup(來源,html.parser")js_test = soup.find('p', class_='jstest')打印(js_test.text)
當我運行它時,我收到消息:
<塊引用>source = client_response.mainFrame().toHtml()AttributeError:客戶端"對象沒有屬性主框架"
我嘗試了幾種不同的解決方案,但都沒有奏效.任何幫助將不勝感激.
編輯
在第 15 行記錄 QUrl(url) 返回此值:
<塊引用>PyQt5.QtCore.QUrl('https://pythonprogramming.net/parsememcparseface/')
當我為第 26 行嘗試 source = client_response.load(QUrl(url))
時,我最終得到以下消息:
文件test3.py",第 28 行,在 <module>湯= BeautifulSoup(來源,html.parser")文件/Users/MYNAME/.venv/qtproject/lib/python3.6/site-packages/bs4/__init__.py",第 192 行,在 __init__elif len(標記) <= 256 和 (TypeError:NoneType"類型的對象沒有 len()
當我嘗試 source = client_response.url()
我得到:
soup = BeautifulSoup(source, "html.parser")文件/Users/MYNAME/.venv/qtproject/lib/python3.6/site-packages/bs4/__init__.py",第 192 行,在 __init__elif len(標記) <= 256 和 (TypeError:QUrl"類型的對象沒有 len()
你必須在類的定義中調用QWebEnginePage::toHtml()
.QWebEnginePage::toHtml()
接受一個指針函數或一個 lambda 作為參數,而這個指針函數又必須接受一個 'str' 類型的參數(這是包含頁面 html 的參數).下面是示例代碼.
將 bs4 導入為 bs導入系統導入 urllib.request從 PyQt5.QtWebEngineWidgets 導入 QWebEnginePage從 PyQt5.QtWidgets 導入 QApplication從 PyQt5.QtCore 導入 QUrl類頁面(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('加載完成')def 可調用(自我,html_str):self.html = html_strself.app.quit()定義主():page = Page('https://pythonprogramming.net/parsememcparseface/')湯 = bs.BeautifulSoup(page.html, 'html.parser')js_test = soup.find('p', class_='jstest')打印 js_test.text如果 __name__ == '__main__': main()
I'm doing Sentdex's PyQt4 YouTube tutorial right here. I'm trying to follow along but use PyQt5 instead. It's a simple web scraping app. I followed along with Sentdex's tutorial and I got here:
Now I'm trying to write the same application with PyQt5 and this is what I have:
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)
When I run this, I get the message:
source = client_response.mainFrame().toHtml() AttributeError: 'Client' object has no attribute 'mainFrame'
I've tried a few different solutions but none work. Any help would be appreciated.
EDIT
Logging QUrl(url) on line 15 returns this value:
PyQt5.QtCore.QUrl('https://pythonprogramming.net/parsememcparseface/')
When I try source = client_response.load(QUrl(url))
for line 26, I end up with the message:
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()
When I try source = client_response.url()
I get:
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()
you must call the QWebEnginePage::toHtml()
inside the definition of the class. QWebEnginePage::toHtml()
takes a pointer function or a lambda as a parameter, and this pointer function must in turn take a parameter of 'str' type (this is the parameter that contains the page's html). Here is sample code below.
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()
這篇關于PyQt4 到 PyQt5 ->mainFrame() 已棄用,需要修復才能加載網頁的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!