botonomus.client¶
The httpx-like Client, Identity, Response and the module-level request functions.
botonomus.client
¶
A lightweight, httpx-like layer over a shared real browser.
import botonomus
response = await botonomus.get("https://example.com")
async with botonomus.Client(max_tabs="auto") as client:
response = await client.get("https://example.com")
async with client.identity("acct-01") as me:
await me.post("https://example.com/login", data={"user": "..."})
The module-level functions open a Client for one request, like httpx.get; keep a
Client open to reuse the browser, cookies and the HTTP fast path.
Client
¶
Client(*, config: BrowserConfig | None = None, proxy: str | None = None, mode: Mode = 'auto', max_tabs: int | Literal['auto'] = 'auto', block: Sequence[str] = (), fresh_context: bool = False, timeout: float = 30.0, wait: Wait = 'load', identity_root: str | Path | None = None)
Bases: _Verbs
A lightweight, httpx-like client backed by one shared, real Chrome.
An additional layer: Botonomus and its profile-backed sessions are unchanged.
The browser starts on the first request and closes with the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BrowserConfig | None
|
Browser settings (executable, persona, proxy, headless, humanize...).
|
None
|
proxy
|
str | None
|
Proxy for this client's contexts (credentials allowed); |
None
|
mode
|
Mode
|
|
'auto'
|
max_tabs
|
int | Literal['auto']
|
Concurrent tabs, or |
'auto'
|
block
|
Sequence[str]
|
Resource types to skip loading: |
()
|
fresh_context
|
bool
|
A new, empty context for every request instead of one shared context (cookies persist across requests by default). |
False
|
timeout
|
float
|
Default seconds per request. |
30.0
|
wait
|
Wait
|
Default page state to wait for: |
'load'
|
identity_root
|
str | Path | None
|
Where |
None
|
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
For an invalid option. |
Example
async with Client() as client: # doctest: +SKIP ... response = await client.get("https://example.com") ... print(response.status, response.via)
request
async
¶
request(method: str, url: str, *, params: Mapping[str, Any] | None = None, headers: Mapping[str, str] | None = None, data: Mapping[str, Any] | str | bytes | None = None, json: Any = None, timeout: float | None = None, wait: Wait | None = None, settle: float = 0.0) -> Response
Send a request and return its Response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP method. |
required |
url
|
str
|
|
required |
params
|
Mapping[str, Any] | None
|
Query parameters appended to |
None
|
headers
|
Mapping[str, str] | None
|
Extra request headers. |
None
|
data
|
Mapping[str, Any] | str | bytes | None
|
Form fields (mapping), or a raw |
None
|
json
|
Any
|
A JSON body (sets |
None
|
timeout
|
float | None
|
Seconds; defaults to the client's. |
None
|
wait
|
Wait | None
|
Page state to wait for in a tab; defaults to the client's. |
None
|
settle
|
float
|
Extra seconds to let scripts run in a tab before reading it. |
0.0
|
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
For invalid arguments. |
ManagerClosedError
|
If the client is closed. |
NavigationError
|
If the browser cannot load the URL. |
TimeoutError_
|
If the page does not reach |
page
async
¶
page(url: str | None = None, *, wait: Wait | None = None, timeout: float | None = None) -> AsyncIterator[Any]
A tab in the client's context for full interaction (click, type, scroll).
Yields a native Page, or a HumanPage when config.humanize is on. The
tab closes on exit; cookies and localStorage stay in the context.
identity
async
¶
identity(name: str, *, proxy: str | None = None, backend: Backend = 'context') -> AsyncIterator[Identity]
A persistent identity: cookies, localStorage and proxy kept between uses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Identity name (profile-name rules). One live use at a time, across processes. |
required |
proxy
|
str | None
|
Proxy for this identity, saved with it; |
None
|
backend
|
Backend
|
|
'context'
|
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
For an invalid name, proxy or backend. |
ProfileInUseError
|
If the identity is in use elsewhere. |
Identity
¶
Identity(client: Client, holder: _Holder, name: str)
Bases: _Verbs
A named, persistent identity of a Client. Obtained from Client.identity.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
The normalized identity name. |
request
async
¶
request(method: str, url: str, *, params: Mapping[str, Any] | None = None, headers: Mapping[str, str] | None = None, data: Mapping[str, Any] | str | bytes | None = None, json: Any = None, timeout: float | None = None, wait: Wait | None = None, settle: float = 0.0) -> Response
Send a request as this identity. See Client.request.
page
async
¶
page(url: str | None = None, *, wait: Wait | None = None, timeout: float | None = None) -> AsyncIterator[Any]
A tab as this identity. See Client.page.
Headers
¶
Bases: Mapping[str, str]
Read-only, case-insensitive response headers; keys are stored in lowercase.
Response
dataclass
¶
Response(url: str, status: int, headers: Headers, content: bytes, via: Via, elapsed: float, method: str = 'GET', html: str | None = None, cookies: dict[str, str] = dict())
One completed request.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
Final URL after redirects. |
status |
int
|
HTTP status code. |
headers |
Headers
|
Response headers. |
content |
bytes
|
The raw response body. |
via |
Via
|
|
elapsed |
float
|
Seconds from sending to having the body. |
method |
str
|
Request method. |
html |
str | None
|
For browser responses, the rendered DOM after scripts ran; else |
cookies |
dict[str, str]
|
Cookie names and values the context holds for this URL afterwards. |
request
async
¶
request(method: str, url: str, *, config: BrowserConfig | None = None, proxy: str | None = None, **kwargs: Any) -> Response
Send one request with a temporary Client. See Client.request.
get
async
¶
get(url: str, **kwargs: Any) -> Response
request("GET", url, ...) with a temporary Client.
post
async
¶
post(url: str, **kwargs: Any) -> Response
request("POST", url, ...) with a temporary Client.
put
async
¶
put(url: str, **kwargs: Any) -> Response
request("PUT", url, ...) with a temporary Client.
patch
async
¶
patch(url: str, **kwargs: Any) -> Response
request("PATCH", url, ...) with a temporary Client.