[英]How to specify user agent in QWebEngineView
I am working on automating functionality on a web page using PyQt5.我正在使用 PyQt5 在网页上实现自动化功能。 The page that is displayed in PyQt5 is substantially different from what is displayed in Chrome.
PyQt5 中显示的页面与 Chrome 中显示的页面有很大不同。 If I was to change the User Agent, could I mimic Chromes functionality?
如果我要更改用户代理,我可以模仿 Chrome 的功能吗? And if so, how would I go about changing the User Agent in the following example:
如果是这样,我将如何更改以下示例中的用户代理:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
web = QWebEngineView()
profile = QWebEngineProfile()
profile.setHttpUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36")
# How do i set the profile in the web ???
web.load(QUrl("https://stackoverflow.com"))
web.show()
web.loadFinished.connect(on_load_finished)
sys.exit(app.exec_())
The User-Agent request header contains a characteristic string that allows the network protocol peers to identify the application type, operating system, software vendor or software version of the requesting software user agent.
User-Agent请求头包含一个特征字符串,允许网络协议对等体识别请求软件用户代理的应用程序类型、操作系统、软件供应商或软件版本。
Some web pages will use the User Agent to display personalized content for your browser, for example, with the user-agent information you could deduce whether it supports AJAX or not.一些网页将使用用户代理为您的浏览器显示个性化内容,例如,您可以通过用户代理信息推断它是否支持 AJAX。
If I was to change the User Agent, could I mimic Chromes functionality?如果我要更改用户代理,我可以模仿 Chrome 的功能吗?
Probably yes, although although Google Chrome and Qt Webengine are based on chromium, each development group has created a new layer that can have different functionalities, for example, QtWebEngine has suppressed many chromium functionalities that are added in the new versions.可能是的,虽然谷歌Chrome和Qt Webengine虽然都是基于chromium,但是每个开发组都创建了一个新的层,可以有不同的功能,比如QtWebEngine就压制了很多新版本中增加的chromium功能。
How would I go about changing the User Agent?我将如何更改用户代理?
It is not necessary to create a new QWebEngineProfile since you can use the profile of the page:没有必要创建新的 QWebEngineProfile,因为您可以使用页面的配置文件:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
if __name__ == "__main__":
app = QApplication(sys.argv)
web = QWebEngineView()
print(web.page().profile().httpUserAgent())
web.page().profile().setHttpUserAgent(
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
)
web.load(QUrl("https://stackoverflow.com"))
web.show()
web.resize(640, 480)
sys.exit(app.exec_())
If you want to use the QWebEngineProfile then create a new QWebEnginePage:如果你想使用 QWebEngineProfile 然后创建一个新的 QWebEnginePage:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineProfile, QWebEngineView
from PyQt5.QtWidgets import QApplication
if __name__ == "__main__":
app = QApplication(sys.argv)
web = QWebEngineView()
profile = QWebEngineProfile()
profile.setHttpUserAgent(
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
)
page = QWebEnginePage(profile, web)
web.setPage(page)
web.load(QUrl("https://stackoverflow.com"))
web.show()
web.resize(640, 480)
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.