what is the Default user-Agent of PyQt Web kit Qwebview and how to get it

人走茶凉 提交于 2019-12-13 02:23:46

问题


i am new to python and developing a GUI in PyQt which has a Web Browser. I want to show the User-Agent going with the Url but not founding a way.my code is -

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")

    # some code like "print headers.get("User-Agent")"

    url = reply.url().toString()
    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    status, ok = status.toInt()
    self.table.update([url, str(status), content_type])

Presently, the above code is showing only the URL,status and content type , but with this i also wants to display user agent.do someone has any idea?


回答1:


A User-Agent is something which gets send to the server. This information is not sent from the server.

To set a user agent you can do the following with your Manager class for example:

from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

manager = Manager()
request = QNetworkRequest(QUrl("http://www.google.com/"))
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1")
manager.get(request)

And modify your def _finished(self, reply): method to get the request with the User-Agent:

def _finished(self, reply):
    print reply.request().rawHeader("User-Agent")


来源:https://stackoverflow.com/questions/36995190/what-is-the-default-user-agent-of-pyqt-web-kit-qwebview-and-how-to-get-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!