简体   繁体   中英

How to specify user agent in QWebEngineView

I am working on automating functionality on a web page using PyQt5. The page that is displayed in PyQt5 is substantially different from what is displayed in Chrome. If I was to change the User Agent, could I mimic Chromes functionality? 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_())

According to the docs :

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.

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.

If I was to change the User Agent, could I mimic Chromes functionality?

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.

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:

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:

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM