Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Set a custom User-Agent for QWebEngine (in PyQt5)
Forum Updated to NodeBB v4.3 + New Features

Set a custom User-Agent for QWebEngine (in PyQt5)

Scheduled Pinned Locked Moved Unsolved QtWebEngine
3 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harborman
    wrote on 1 Oct 2021, 19:55 last edited by Harborman 10 Mar 2021, 15:13
    #1

    I read a couple of questions on how to set a custom user-agent for your browser, but couldn't make it work.
    Mainly I followed these steps:
    https://stackoverflow.com/questions/5317924/how-do-i-set-the-user-agent-for-a-qnetworkrequest-in-pyqtwebkit
    https://pastebin.com/m1b350244
    and subclassed QWebEnginePage with a userAgentForUrl function as they instructed. My Apache server has been rigged to let you pass only if your user-agent is "Ree", but I get a 403 Forbidden error, so I think this code doesn't send the user-agent:

    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtWebEngineWidgets import *
    
    USER_AGENT = "Ree"
    
    class WebPage(QWebEnginePage):
        def __init__(self):
            super(QWebEnginePage, self).__init__()
    
        def userAgentForUrl(self, url):
            return USER_AGENT
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.resize(1024, 800)
    
            widget = QWidget()
            self.setCentralWidget(widget)
    
            self.webview = QWebEngineView(self)
            self.page = WebPage()
            self.webview.setPage(self.page)
    
            vbox = QVBoxLayout()
            vbox.addWidget(self.webview)
            widget.setLayout(vbox)
    
        def loadUrl(self, url):
            self.webview.load(QUrl(url))
            self.webview.show()
    
    if __name__ == "__main__":
        import sys
    
        app = QApplication(sys.argv)
        window = MainWindow()
        window.loadUrl("http://127.0.0.1")
        window.show()
    
        sys.exit(app.exec_())
    

    Does it have something to do with the fact that userAgentForUrl function takes a url but it doesn't receive one?

    E 1 Reply Last reply 1 Oct 2021, 19:59
    0
    • H Harborman
      1 Oct 2021, 19:55

      I read a couple of questions on how to set a custom user-agent for your browser, but couldn't make it work.
      Mainly I followed these steps:
      https://stackoverflow.com/questions/5317924/how-do-i-set-the-user-agent-for-a-qnetworkrequest-in-pyqtwebkit
      https://pastebin.com/m1b350244
      and subclassed QWebEnginePage with a userAgentForUrl function as they instructed. My Apache server has been rigged to let you pass only if your user-agent is "Ree", but I get a 403 Forbidden error, so I think this code doesn't send the user-agent:

      from PyQt5.QtCore import *
      from PyQt5.QtWidgets import *
      from PyQt5.QtWebEngineWidgets import *
      
      USER_AGENT = "Ree"
      
      class WebPage(QWebEnginePage):
          def __init__(self):
              super(QWebEnginePage, self).__init__()
      
          def userAgentForUrl(self, url):
              return USER_AGENT
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super(MainWindow, self).__init__()
              self.resize(1024, 800)
      
              widget = QWidget()
              self.setCentralWidget(widget)
      
              self.webview = QWebEngineView(self)
              self.page = WebPage()
              self.webview.setPage(self.page)
      
              vbox = QVBoxLayout()
              vbox.addWidget(self.webview)
              widget.setLayout(vbox)
      
          def loadUrl(self, url):
              self.webview.load(QUrl(url))
              self.webview.show()
      
      if __name__ == "__main__":
          import sys
      
          app = QApplication(sys.argv)
          window = MainWindow()
          window.loadUrl("http://127.0.0.1")
          window.show()
      
          sys.exit(app.exec_())
      

      Does it have something to do with the fact that userAgentForUrl function takes a url but it doesn't receive one?

      E Offline
      E Offline
      eyllanesc
      wrote on 1 Oct 2021, 19:59 last edited by eyllanesc 10 Jan 2021, 20:00
      #2

      @Harborman userAgentForUrl is for QWebPage from QtWebkit, for QtWebEngine you must use QWebEngineProfile::setHttpUserAgent():

      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()
          web.page().profile().setHttpUserAgent("Ree")
          web.load(QUrl("http://127.0.0.1"))
          web.show()
          web.resize(640, 480)
          sys.exit(app.exec_())
      

      Also see https://stackoverflow.com/questions/57828992/how-to-specify-user-agent-in-qwebengineview/57829384#57829384

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      H 1 Reply Last reply 3 Oct 2021, 15:12
      3
      • E eyllanesc
        1 Oct 2021, 19:59

        @Harborman userAgentForUrl is for QWebPage from QtWebkit, for QtWebEngine you must use QWebEngineProfile::setHttpUserAgent():

        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()
            web.page().profile().setHttpUserAgent("Ree")
            web.load(QUrl("http://127.0.0.1"))
            web.show()
            web.resize(640, 480)
            sys.exit(app.exec_())
        

        Also see https://stackoverflow.com/questions/57828992/how-to-specify-user-agent-in-qwebengineview/57829384#57829384

        H Offline
        H Offline
        Harborman
        wrote on 3 Oct 2021, 15:12 last edited by
        #3

        @eyllanesc Thank you friend, that works. This question is solved. I also found the "code" button so I won't paste code here without indents in the future :-).

        1 Reply Last reply
        0

        1/3

        1 Oct 2021, 19:55

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved