Python Quick Start
Get started with the Browser7 Python SDK in minutes.
Prerequisites
- Python 3.8+
- A Browser7 API key from the Dashboard
Synchronous Usage
Use Browser7 for scripts, data pipelines, and Jupyter notebooks.
from browser7 import Browser7
client = Browser7(api_key='b7_your_api_key_here')
result = client.render('https://example.com')
print(result.html)
Asynchronous Usage
Use AsyncBrowser7 for FastAPI, Django async views, or any asyncio-based application.
import asyncio
from browser7 import AsyncBrowser7
async def main():
client = AsyncBrowser7(api_key='b7_your_api_key_here')
result = await client.render('https://example.com')
print(result.html)
asyncio.run(main())
Geo-Targeting
Target a specific country or city for rendering.
# Sync
result = client.render(
'https://example.com',
country_code='US',
city='new.york'
)
# Async
result = await client.render(
'https://example.com',
country_code='GB',
city='london'
)
See Supported Countries for all available country codes and cities.
Wait Actions
Control how the browser waits for dynamic content before capturing the page.
from browser7 import Browser7, wait_for_click, wait_for_selector, wait_for_text
client = Browser7(api_key='b7_your_api_key_here')
result = client.render(
'https://example.com',
wait_for=[
wait_for_click('.cookie-accept'), # Dismiss cookie banner
wait_for_selector('.main-content'), # Wait for content to appear
wait_for_text('In Stock', '.availability'), # Wait for specific text
]
)
Screenshots
# Viewport screenshot (JPEG)
result = client.render(
'https://example.com',
include_screenshot=True
)
# Full page PNG screenshot
result = client.render(
'https://example.com',
include_screenshot=True,
screenshot_format='png',
screenshot_full_page=True
)
# Access the base64-encoded image
print(result.screenshot)
Performance Options
Block images for faster renders (enabled by default):
result = client.render(
'https://example.com',
block_images=True # Default - skip this for image-heavy pages
)
CAPTCHA Solving
# Auto-detect and solve any CAPTCHA
result = client.render(
'https://example.com',
captcha='auto'
)
# Target a specific CAPTCHA type
result = client.render(
'https://example.com',
captcha='recaptcha_v2'
)
See CAPTCHA Solving for more.
Progress Callbacks
Track render progress with a callback function.
Sync callback
def on_progress(event):
print(f"[{event['type']}] {event.get('status', '')}")
result = client.render('https://example.com', on_progress=on_progress)
Async callback
The async client accepts both sync and async callbacks:
async def on_progress(event):
print(f"[{event['type']}] {event.get('status', '')}")
await log_to_db(event) # Awaitable operations are supported
result = await client.render('https://example.com', on_progress=on_progress)
Context Managers
Use context managers for automatic connection cleanup.
# Sync
with Browser7(api_key='b7_your_api_key_here') as client:
result = client.render('https://example.com')
# Async
async with AsyncBrowser7(api_key='b7_your_api_key_here') as client:
result = await client.render('https://example.com')
Error Handling
try:
result = client.render('https://example.com')
print(result.html)
except Exception as e:
print(f'Error: {e}')
RenderResult
A successful render returns a RenderResult object:
result.status # 'completed', 'processing', or 'failed'
result.html # Rendered HTML (automatically decompressed)
result.screenshot # Base64-encoded image (if include_screenshot=True)
result.selected_city # City used for rendering
result.bandwidth_metrics # Network stats
result.captcha # CAPTCHA detection info
result.timing_breakdown # Performance timings
result.fetch_responses # Additional fetch responses (if fetch_urls provided)
Next Steps
- API Reference - Complete method and type documentation
- Examples - Copy-paste ready examples for common use cases
- Advanced API - Direct REST API usage