Python
Learn about Hyperbrowser's Python SDK
View on Github
Installation
pip install hyperbrowser
Usage
Hyperbrowser's Python SDK includes both a sync and async client.
Async
import os
from dotenv import load_dotenv
import asyncio
from pyppeteer import connect
from hyperbrowser import AsyncHyperbrowser
load_dotenv()
client = AsyncHyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))
async def main():
session = await client.sessions.create()
ws_endpoint = session.ws_endpoint
browser = await connect(browserWSEndpoint=ws_endpoint, defaultViewport=None)
# Get pages
pages = await browser.pages()
if not pages:
raise Exception("No pages available")
page = pages[0]
# Navigate to a website
print("Navigating to Hacker News...")
await page.goto("https://news.ycombinator.com/")
page_title = await page.title()
print("Page title:", page_title)
await page.close()
await browser.disconnect()
await client.sessions.stop(session.id)
print("Session completed!")
# Run the asyncio event loop
asyncio.get_event_loop().run_until_complete(main())
Sync
import os
from dotenv import load_dotenv
from playwright.sync_api import sync_playwright
from hyperbrowser import Hyperbrowser
load_dotenv()
client = Hyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))
def main():
session = client.sessions.create()
ws_endpoint = session.ws_endpoint
# Launch Playwright and connect to the remote browser
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(ws_endpoint)
context = browser.new_context()
# Get the first page or create a new one
if len(context.pages) == 0:
page = context.new_page()
else:
page = context.pages[0]
# Navigate to a website
print("Navigating to Hacker News...")
page.goto("https://news.ycombinator.com/")
page_title = page.title()
print("Page title:", page_title)
page.close()
browser.close()
print("Session completed!")
client.sessions.stop(session.id)
main()
Last updated