• <tfoot id='GhS7j'></tfoot>
    1. <i id='GhS7j'><tr id='GhS7j'><dt id='GhS7j'><q id='GhS7j'><span id='GhS7j'><b id='GhS7j'><form id='GhS7j'><ins id='GhS7j'></ins><ul id='GhS7j'></ul><sub id='GhS7j'></sub></form><legend id='GhS7j'></legend><bdo id='GhS7j'><pre id='GhS7j'><center id='GhS7j'></center></pre></bdo></b><th id='GhS7j'></th></span></q></dt></tr></i><div id='GhS7j'><tfoot id='GhS7j'></tfoot><dl id='GhS7j'><fieldset id='GhS7j'></fieldset></dl></div>
    2. <legend id='GhS7j'><style id='GhS7j'><dir id='GhS7j'><q id='GhS7j'></q></dir></style></legend>

          <bdo id='GhS7j'></bdo><ul id='GhS7j'></ul>

        <small id='GhS7j'></small><noframes id='GhS7j'>

      1. PyQt4 到 PyQt5 ->mainFrame() 已弃用,需要修复才能

        时间:2023-08-06

        <legend id='xDNmD'><style id='xDNmD'><dir id='xDNmD'><q id='xDNmD'></q></dir></style></legend>

            <tbody id='xDNmD'></tbody>

            • <i id='xDNmD'><tr id='xDNmD'><dt id='xDNmD'><q id='xDNmD'><span id='xDNmD'><b id='xDNmD'><form id='xDNmD'><ins id='xDNmD'></ins><ul id='xDNmD'></ul><sub id='xDNmD'></sub></form><legend id='xDNmD'></legend><bdo id='xDNmD'><pre id='xDNmD'><center id='xDNmD'></center></pre></bdo></b><th id='xDNmD'></th></span></q></dt></tr></i><div id='xDNmD'><tfoot id='xDNmD'></tfoot><dl id='xDNmD'><fieldset id='xDNmD'></fieldset></dl></div>
            • <small id='xDNmD'></small><noframes id='xDNmD'>

              • <bdo id='xDNmD'></bdo><ul id='xDNmD'></ul>
                <tfoot id='xDNmD'></tfoot>
                  本文介绍了PyQt4 到 PyQt5 ->mainFrame() 已弃用,需要修复才能加载网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在学习 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模板网!

                  上一篇:当我将 PyQt QProgressBar 与 Selenuim 一起使用时,它不 下一篇:多重继承元类冲突

                  相关文章

                  最新文章

                  <i id='Rs9V4'><tr id='Rs9V4'><dt id='Rs9V4'><q id='Rs9V4'><span id='Rs9V4'><b id='Rs9V4'><form id='Rs9V4'><ins id='Rs9V4'></ins><ul id='Rs9V4'></ul><sub id='Rs9V4'></sub></form><legend id='Rs9V4'></legend><bdo id='Rs9V4'><pre id='Rs9V4'><center id='Rs9V4'></center></pre></bdo></b><th id='Rs9V4'></th></span></q></dt></tr></i><div id='Rs9V4'><tfoot id='Rs9V4'></tfoot><dl id='Rs9V4'><fieldset id='Rs9V4'></fieldset></dl></div>

                    <bdo id='Rs9V4'></bdo><ul id='Rs9V4'></ul>
                • <tfoot id='Rs9V4'></tfoot>

                  1. <legend id='Rs9V4'><style id='Rs9V4'><dir id='Rs9V4'><q id='Rs9V4'></q></dir></style></legend>

                    <small id='Rs9V4'></small><noframes id='Rs9V4'>