How to execute QWebEngine in Python function

╄→尐↘猪︶ㄣ 提交于 2019-11-30 21:39:58

问题


I have a QWebEngine class tor read webpages and create BeautifulSoup for them.

Here is the code:

import sys
from bs4 import BeautifulSoup
import os


from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


class WebPage(QtWebEngineWidgets.QWebEnginePage):
    def __init__(self):
        super(WebPage, self).__init__()
        self.loadFinished.connect(self.handleLoadFinished)
        self.soup = []

    def start(self, urls):
        self._urls = iter(urls)
        self.fetchNext()

    def fetchNext(self):
        try:
            url = next(self._urls)
        except StopIteration:
            return False
        else:
            self.load(QtCore.QUrl(url))
        return True

    def processCurrentPage(self, html):
        url = self.url().toString()
        self.soup.append(BeautifulSoup(html, 'lxml'))
        if not self.fetchNext():
            QtWidgets.qApp.quit()

    def handleLoadFinished(self):
        self.toHtml(self.processCurrentPage)

Here is another function to call WebPage class:

def get_soup(urls):
    app = QtWidgets.QApplication(sys.argv)
    webpage = WebPage()
    webpage.start(urls)
    return webpage.soup

Here is the main:

if __name__ == "__main__":

    urls = ["http://www.hkexnews.hk/sdw/search/mutualmarket_c.aspx?t=sh", "http://www.hkexnews.hk/sdw/search/mutualmarket_c.aspx?t=sz"]      
    soups = get_soup(urls)

However, the program restarts when I executed the program.

What should be changed?


回答1:


This is a problem that I had already had and analyzing I found that the QApplication is destroyed before QWebEnginePage making the QWebEngineProfile is deleted, and in this case causing QWebEnginePage crashes. The solution is to make the app have a greater scope by making it a global variable.

On the other hand you have to call exec_() so that the eventloop that allows the operation of the signals

# ...
app = None

def get_soup(urls):
    global app
    app = QtWidgets.QApplication(sys.argv)
    webpage = WebPage()
    webpage.start(urls)
    app.exec_()
    return webpage.soup
# ...

Note: It seems that the QTBUG-75547 related to this problem has been solved for Qt5>=5.12.4 so probably in a next release of PyQtWebEngine that bug will no longer be observed.



来源:https://stackoverflow.com/questions/56627463/how-to-execute-qwebengine-in-python-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!