Python 在函数或类中使用QApplication::exec()

Python 在函数或类中使用QApplication::exec(),python,qt,pyqt,qt5,pyqt5,Python,Qt,Pyqt,Qt5,Pyqt5,我希望下面的代码能够获取以http://down.51en.com:88在web加载过程中,然后对url的响应对象执行其他处理。 在我的程序中,一旦targetUrl被赋值,我希望函数targetUrlGetter(url)将其返回给调用者,但是问题是QApplication::exec()进入主事件循环,因此无法在exec()调用后的targetUrlGetter()函数末尾执行代码,因此,函数无法返回,我在interceptRequest(self,info)中尝试了qApp.quit(),

我希望下面的代码能够获取以
http://down.51en.com:88
在web加载过程中,然后对url的响应对象执行其他处理。 在我的程序中,一旦
targetUrl
被赋值,我希望函数
targetUrlGetter(url)
将其返回给调用者,但是问题是
QApplication::exec()
进入主事件循环,因此无法在exec()调用后的
targetUrlGetter()
函数末尾执行代码,因此,函数无法返回,我在
interceptRequest(self,info)
中尝试了
qApp.quit()
,以便告诉应用程序退出,以便
targetUrlGetter(url)
可以返回,但函数仍然无法返回,程序甚至在退出时崩溃(在Win7 32位上测试),那么,如何将
targetUrl
返回给调用者程序呢

这里的困难是如何在不崩溃的情况下退出Qt事件循环,并将请求url返回给调用者

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtCore import *


class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.page = parent

    def interceptRequest(self, info):
        if info.requestUrl().toString().startswith('http://down.51en.com:88'):
            self.targetUrl = info.requestUrl().toString()
            print('----------------------------------------------', self.targetUrl)
            qApp.quit()

            # self.page.load(QUrl(''))


def targetUrlGetter(url=None):
    app = QApplication(sys.argv)
    page = QWebEnginePage()
    globalSettings = page.settings().globalSettings()
    globalSettings.setAttribute(
        QWebEngineSettings.PluginsEnabled, True)
    globalSettings.setAttribute(
        QWebEngineSettings.AutoLoadImages, False)
    profile = page.profile()
    webEngineUrlRequestInterceptor = WebEngineUrlRequestInterceptor(page)
    profile.setRequestInterceptor(webEngineUrlRequestInterceptor)
    page.load(QUrl(url))
    # view = QWebEngineView()
    # view.setPage(page)
    # view.show()
    app.exec_()
    return webEngineUrlRequestInterceptor.targetUrl


url = "http://www.51en.com/news/sci/everything-there-is-20160513.html"
# url = "http://www.51en.com/news/sci/obese-dad-s-sperm-may-influence-offsprin.html"
# url = "http://www.51en.com/news/sci/mars-surface-glass-could-hold-ancient-fo.html"
targetUrl = targetUrlGetter(url)
print(targetUrl)

您应该始终在程序开始时初始化
QApplication
,并始终在程序结束时调用
QApplication::exec
函数

另一件事是
QWebEngineUrlRequestInterceptor.interceptRequest
是一个异步调用的回调函数。由于在回调函数中调用了
info.requestUrl().toString()
,因此无法
同步返回结果

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtCore import *


class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.page = parent

    def interceptRequest(self, info):
        if info.requestUrl().toString().startswith('http://down.51en.com:88'):
            self.targetUrl = info.requestUrl().toString()
            print('----------------------------------------------', self.targetUrl)
            # Don't do thatDo you really want to exit the whole program?
            # qApp.quit()

            # Do something here, rather than return it.
            # It must be run asynchronously

            # self.page.load(QUrl(''))


def targetUrlGetter(url=None):
    page = QWebEnginePage()
    globalSettings = page.settings().globalSettings()
    globalSettings.setAttribute(
        QWebEngineSettings.PluginsEnabled, True)
    globalSettings.setAttribute(
        QWebEngineSettings.AutoLoadImages, False)
    profile = page.profile()
    webEngineUrlRequestInterceptor = WebEngineUrlRequestInterceptor(page)
    profile.setRequestInterceptor(webEngineUrlRequestInterceptor)
    page.load(QUrl(url))
    # view = QWebEngineView()
    # view.setPage(page)
    # view.show()

    # Don't return this. It cannot be called synchronously. It must be called asynchronously.
    # return webEngineUrlRequestInterceptor.targetUrl

app = QApplication(sys.argv)  # always initialize QApplication at the beginning of the program
url = "http://www.51en.com/news/sci/everything-there-is-20160513.html"
# url = "http://www.51en.com/news/sci/obese-dad-s-sperm-may-influence-offsprin.html"
# url = "http://www.51en.com/news/sci/mars-surface-glass-could-hold-ancient-fo.html"
targetUrl = targetUrlGetter(url)
print(targetUrl)
app.exec_() # always call the QApplication::exec at the end of the program

我无法复制这个。脚本运行时不会出错,并按预期打印/返回请求的url。我看不出有什么理由
exec()
应该“挂起”。你确定没有网络问题吗?@ekhumaro很抱歉我的英语很差,请再次查看更新的帖子!如果看不到退出崩溃,请在Windows上多次测试代码我无法在Windows上测试。该脚本在Linux上运行良好。您需要进行一些基本调试,以查看脚本失败的地方。网页加载是否正确?使用该选项进行检查。
interceptRequest
是否曾经被调用过,如果是,打印(repr(info.requestUrl().toString())
的输出是什么?
if
语句的计算结果是否为
True
?@ekhumoro除了使用
app
和/或
页面的全局变量退出crashTry外,其他操作都正常。可能存在垃圾收集问题(即对象以错误的顺序删除)。