在QWebview浏览器Pyqt Python中显示标题

时间:2016-04-29 10:26:50

标签: python pyqt qwebview httpfox

我正在PyQt python中构建一个桌面应用程序并加载了一个Web浏览器,现在我想添加http fox(Firefox插件)的功能来查看已传递请求的加载URL以及与每个相关联的其他头文件与http fox中的URL相同。

我已经编写了显示已加载网址的代码,但没有找到在点击每个网址时显示其他标题的方法。我在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])

我想要类似的东西 -

[![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 个答案:

答案 0 :(得分: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()