Search code examples
pythonpyside2pyside6qwebengineview

how can I setting the user-agent when in pyside6 WebEngineView


I wrote a Windows software in python and pyside6, in which I opened an interface with QwebEngine, is there any way to define the information such as this embedded browser request header

here are my code of this part

self.webEngineView = QWebEngineView()
self.initialUrl = 'http://www.bing.com'
self.webEngineView.load(QUrl(self.initialUrl))
self.right_bottom_layout.addWidget(self.webEngineView, 0, 0)

but in windows appplication the web interface is too big is to big and I want to show a mobile web page

enter image description here


Solution

  • You would just need to load a QWebEngineHttpRequest with the modified user agent header instead of a url

    for example

    from PySide6.QtWebEngineCore import QWebEngineHttpRequest
    
    
    self.webEngineView = QWebEngineView()
    self.initialUrl = 'http://www.bing.com'
    
    
    user_agent = b"Mozilla/5.0 (Android 7.0; Mobile; rv:54.0) Gecko/54.0 Firefox/54.0"
    request = QWebEngineHttpRequest()
    request.setUrl(self.initialUrl)
    request.setHeader(b'user-agent',user_agent)
    
    
    self.webEngineView.load(request)
    self.right_bottom_layout.addWidget(self.webEngineView, 0, 0)