2016-04-29 47 views
0

您好我正在PyQt python中構建一個桌面應用程序,並在其中加載了一個Web瀏覽器,現在我想添加http fox(Firefox插件)的功能來查看加載的URL與請求傳遞和其他與每個URL相關的標頭與http狐相同。在QWebview瀏覽器中顯示標題Pyqt Python

我已經編寫了顯示加載網址的代碼,但未找到單擊每個網址時顯示其他標頭的方法。我聽說過Qwebview中的Cookie Jar,但不知道如何顯示每個加載的URL。

我的代碼是: -

class Manager(QNetworkAccessManager): 
def __init__(self, table): 
    QNetworkAccessManager.__init__(self) 
    self.finished.connect(self._finished) 
    self.table = table 

def _finished(self, reply): 
    headers = reply.rawHeaderPairs() 
    headers = {str(k):str(v) for k,v in headers} 
    content_type = headers.get("Content-Type") 
    url = reply.url().toString() 
    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) 
    status, ok = status.toInt() 
    self.table.update([url, str(status), content_type]) 

我想somethinglike-

[![here on the upper part we have loaded URLs and below that we can see the header, i have written the code for loaded URLs but how to show the headers][1]][1] 

回答

1

這是你在找什麼?

import logging 
import sys 
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest 
from PyQt4.QtCore import QUrl, QEventLoop 

log = logging.getLogger(__name__) 


class Manager(QNetworkAccessManager): 
    def __init__(self, table=list()): 
     QNetworkAccessManager.__init__(self) 
     self.finished.connect(self._finished) 
     self.table = table 

    def _finished(self, reply): 
     headers = reply.rawHeaderPairs() 
     headers = {str(k): str(v) for k, v in headers} 
     content_type = headers.get("Content-Type") 
     url = reply.url().toString() 
     status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) 
     status, ok = status.toInt() 
     self.table.append([str(url), str(status), content_type]) 
     log.info(self.table) 
     request = reply.request() 
     log.info(request.rawHeader("User-Agent")) 
     method = reply.operation() 
     if method == QNetworkAccessManager.GetOperation: 
      log.info("get") 
      request.url().queryItems() 
     if method == QNetworkAccessManager.PostOperation: 
      log.info("post") 

def test(): 
    manager = Manager() 
    log.info("Sending request") 
    manager.get(QNetworkRequest(QUrl("http://www.google.com/"))) 

    # just for testing purpose to wait for the request to finish 
    l = QEventLoop() 
    manager.finished.connect(l.quit) 
    l.exec_() 

if __name__ == '__main__': 
    logging.basicConfig(level=logging.DEBUG) 

    app = QApplication(sys.argv) 
    test() 
+0

感謝您的幫助,我也想知道,如何讓用戶代理與請求去,我試圖「打印reply.rawHeader(」用戶代理「)」,但返回一個空字符串。 –

+0

你會幫我,我也提出了這個問題http://stackoverflow.com/questions/36995190/how-to-get-user-agent-and-cookies-going-with-a-request-in -qwebview-pyqt-python –

+0

在您發佈的代碼中,是否可以查看Request方法(「GET」或「POST」)併發布參數(如果有其他頭文件)? –