问题
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