2016-08-06 56 views
0

我希望以下代码在网页加载过程中获取以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) 
+0

我不能重现这一点。脚本运行时没有错误,并按预期打印/返回请求的URL。我不明白为什么'exec()'应该“挂起”。你确定没有网络问题吗? – ekhumoro

+0

@ekhumoro对不起,我可怜的英语,再次看到更新的帖子!如果看不到退出崩溃,请在Windows上多次测试代码 – iMath

+0

我无法在Windows上测试。该脚本在Linux上正常工作。您需要进行一些基本的调试以查看脚本失败的位置。网页加载是否正确?使用[loadFinished signal](http://doc.qt.io/qt-5/qwebenginepage.html#loadFinished)进行检查。 'interceptRequest'是否被调用,如果是,'print(repr(info.requestUrl()。toString()))'的输出是什么? if语句是否评估为“真”? – ekhumoro

回答

-1

你应该总是在程序的开头初始化QApplication,并始终调用QApplication::exec功能在节目的结尾。

另一件事是QWebEngineUrlRequestInterceptor.interceptRequest是一个异步调用的回调函数。由于info.requestUrl().toString()在回调函数中被调用,所以没有办法return它的结果是同步的。

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 
+0

这是一个错误的答案 – iMath

相关问题