我希望以下代码在网页加载过程中获取以http://down.51en.com:88
开头的请求url,然后使用url的响应对象执行其他处理。 在我的程序中,一旦给targetUrl
分配了一个值,我希望函数targetUrlGetter(url)
将其返回给调用者,但问题是QApplication::exec()
进入主事件循环,因此无法在执行exec之后的targetUrlGetter()
函数结束时执行代码()调用,因此函数不能返回,我试着用qApp.quit()
在interceptRequest(self, info)
为了告诉程序退出,所以targetUrlGetter(url)
可以返回,但是函数仍然不能返回,程序甚至在退出时崩溃(在Win7 32bit上测试过) ,那么我怎样才能将targetUrl
返回给调用者程序呢?在函数或类中使用QApplication :: exec()
这里的困难是如何在不发生崩溃的情况下退出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)
我不能重现这一点。脚本运行时没有错误,并按预期打印/返回请求的URL。我不明白为什么'exec()'应该“挂起”。你确定没有网络问题吗? – ekhumoro
@ekhumoro对不起,我可怜的英语,再次看到更新的帖子!如果看不到退出崩溃,请在Windows上多次测试代码 – iMath
我无法在Windows上测试。该脚本在Linux上正常工作。您需要进行一些基本的调试以查看脚本失败的位置。网页加载是否正确?使用[loadFinished signal](http://doc.qt.io/qt-5/qwebenginepage.html#loadFinished)进行检查。 'interceptRequest'是否被调用,如果是,'print(repr(info.requestUrl()。toString()))'的输出是什么? if语句是否评估为“真”? – ekhumoro