Browser Automation
Hrequests supports both Firefox and Chrome browsers, headless and headful sessions.
It is recommended to use Firefox instead. Chrome does not support fingerprint rotation, mocking human mouse movements, or browser extensions.
Usage
You can spawn a BrowserSession
instance by calling it:
>>> page = hrequests.BrowserSession() # headless=True by default
By default, BrowserSession
returns a Firefox browser.
To create a Firefox session, use the chrome shortcut instead:
>>> page = hrequests.chrome.BrowserSession()
BrowserSession
is entirely safe to use across threads.
Camoufox Integration
If you are using a Firefox BrowserSession, you can pass additional parameters to Camoufox:
>>> page = hrequests.BrowserSession(window=(1024, 768), block_images=True, addons=['/path/to/addon'], ...)
You can find a full list of parameters for Camoufox here.
Engine
The engine
parameter allows you to pass in an existing BrowserEngine
instance. This can be useful if you want to reuse a Playwright engine to save time on startup. It is completely threadsafe.
>>> engine = hrequests.BrowserEngine()
Use the same engine for multiple sessions:
>>> page1 = hrequests.BrowserSession(engine=engine)
>>> page2 = hrequests.BrowserSession(engine=engine)
Render an existing Response
Responses have a .render()
method. This will render the contents of the response in a browser page.
Once the page is closed, the Response content and the Response's session cookies will be updated.
Simple usage
Rendered browser sessions will use the browser set in the initial request.
You can set a request's browser with the browser
parameter in the hrequests.get
method:
>>> resp = hrequests.get('https://example.com')
Or by setting the browser
parameter of the hrequests.Session
object:
>>> session = hrequests.Session()
>>> resp = session.get('https://example.com')
Example - submitting a login form:
>>> session = hrequests.Session()
>>> resp = session.get('https://www.somewebsite.com/')
>>> with resp.render(mock_human=True) as page:
... page.find('.input#username').type('myuser')
... page.find('.input#password').type('p4ssw0rd')
... page.click('#submit')
# `session` & `resp` now have updated cookies, content, etc.
Or, without a context manager
>>> session = hrequests.Session()
>>> resp = session.get('https://www.somewebsite.com/')
>>> page = resp.render(mock_human=True)
>>> page.find('.input#username').type('myuser')
>>> page.find('.input#password').type('p4ssw0rd')
>>> page.click('#submit')
>>> page.close() # Must close the page when done!
The mock_human
parameter will emulate human-like behavior. This includes easing and randomizing mouse movements, and randomizing typing speed.
Other ways to create a Browser Session
You can use .render
to spawn a BrowserSession
object directly from a url:
# Using a Session:
>>> page = session.render('https://google.com')
# Or without a session at all:
>>> page = hrequests.render('https://google.com')
Make sure to close all BrowserSession
objects when done!
>>> page.close()
Last updated