使用QWebView请求的POST请求

时间:2012-10-22 07:33:05

标签: python pyqt pyside qwebview qwebkit

您好我正在使用pyqt4,我想要做的是使用我的QWebPage / QWebView的QtWebKit请求来制作任意POST请求。通过广泛了解我的理解是,我必须通过覆盖createRequest()方法来继承networkaccessmanager。

我看到一个示例允许使用createRequest的第四个参数来提取正在发送的POST请求的数据(请参阅下面的代码),但我的问题是我如何设置createRequest的相同数据变量来发送自定义POST数据,例如" query = myvalue"?

显然数据是一个QIODevice变量:我尝试了很多不同的方法来设置它,但是不能让它工作并总是以python抱怨它是一个意想不到的值。我在这里错过了什么吗?有人愿意分享工作代码样本吗?

import sys  
from PySide.QtCore import *  
from PySide.QtGui import QApplication  
from PySide.QtWebKit import QWebView, QWebPage 
from PySide.QtNetwork import QNetworkAccessManager

html = '''  <html>  <body> 
    <form action="http://www.google.com" method="post"><input type="text" name="test" /><input type="submit" value="submit"/></form>
</body>  </html>  ''' 

class Browser(object):

    def __init__(self):
        self.network_manager = QNetworkAccessManager()
        self.network_manager.createRequest = self._create_request

        self.web_page = QWebPage()
        self.web_page.setNetworkAccessManager(self.network_manager)

        self.web_view = QWebView()
        self.web_view.setPage(self.web_page)

        self.html_data = None

    def _create_request(self, operation, request, data):
        # data contains all the post data that is being added to the request
        # so you can look into it here
        print data.readAll()
        reply = QNetworkAccessManager.createRequest(self.network_manager,
                                                    operation,
                                                    request,
                                                    data)
        return reply

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    browser = Browser() 
    frame = browser.web_page.mainFrame() 
    browser.web_view.setHtml(html) 
    browser.web_view.show() 
    app.exec_()

1 个答案:

答案 0 :(得分:3)

您发布的代码来自我的一个答案,所以我也有义务回答这个问题:)

from PySide.QtCore import QByteArray, QUrl
from PySide.QtGui import QApplication  
from PySide.QtWebKit import QWebView, QWebPage 
from PySide.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply


class Browser(object):

    def __init__(self):
        self.network_manager = QNetworkAccessManager()
        self.network_manager.createRequest = self._create_request
        self.network_manager.finished.connect(self._request_finished)

        self.web_page = QWebPage()
        self.web_page.setNetworkAccessManager(self.network_manager)

        self.web_view = QWebView()
        self.web_view.setPage(self.web_page)

    def _create_request(self, operation, request, data):
        print data.readAll()
        reply = QNetworkAccessManager.createRequest(self.network_manager,
                                                    operation,
                                                    request,
                                                    data)
        return reply

    def _request_finished(self, reply):
        if not reply.error() == QNetworkReply.NoError:
            # request probably failed
            print reply.error()
            print reply.errorString()

    def _make_request(self, url):
        request = QNetworkRequest()
        request.setUrl(QUrl(url))
        return request

    def _urlencode_post_data(self, post_data):
        post_params = QUrl()
        for (key, value) in post_data.items():
            post_params.addQueryItem(key, unicode(value))

        return post_params.encodedQuery()

    def perform(self, url, method='GET', post_data=dict()):
        request = self._make_request(url)

        if method == 'GET':
            self.web_view.load(request)
        else:
            encoded_data = self._urlencode_post_data(post_data)
            request.setRawHeader('Content-Type',
                                 QByteArray('application/x-www-form-urlencoded'))
            self.web_view.load(request,
                               QNetworkAccessManager.PostOperation,
                               encoded_data)

if __name__ == '__main__':
    app = QApplication([])
    browser = Browser()
    browser.perform('http://www.python.org', 'POST', {'test': 'value', 'anothername': 'gfdgfd'})
    app.exec_()