[PyQt] QtWebEngineProfile and http user agent

me at alandmoore.com me at alandmoore.com
Fri Jul 24 17:25:28 BST 2015


I'm working on porting a QtWebkit based project
(http://github.com/alandmoore/wcgbrowser) to QtWebEngine
(http://github.com/alandmoore/admbrowser).

With pyqt5.5 released, I'm attempting to get custom user-agent strings
working.  If I understand the documentation correctly, I should be able to
set this in the QWebEnginePage's QWebEngineProfile, like in the snippet
below.


Unfortunatley, it doesn't seem to work, and the default agent is both
printed to stdout and sent to the server.

What am I doing wrong?  I'm using pyqt5.5 on python 3.4.3 on Arch Linux.

## START SNIPPET

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import (
    QWebEnginePage, QWebEngineView, QWebEngineProfile)
from PyQt5.QtWidgets import QApplication
import sys


class MyBrowser(QWebEngineView):

    def __init__(self):
        super(MyBrowser, self).__init__()

        profile = QWebEngineProfile()
        profile.setHttpUserAgent("test browser")
        page = QWebEnginePage(profile)
        self.setPage(page)
        print(
            '''Set user agent "{}"'''
            .format(
                self.page().profile().httpUserAgent()
            )
        )

app = QApplication(sys.argv)
m = MyBrowser()
m.show()
m.load(QUrl(sys.argv[1]))
app.exec_()

## END SNIPPET




More information about the PyQt mailing list