A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.hyperbrowser.ai/reference/sdks/python below:

Python | Hyperbrowser

Python | Hyperbrowser
  1. reference
  2. SDKs
Python

Learn about Hyperbrowser's Python SDK

Hyperbrowser's Python SDK includes both a sync and async client.

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())
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 7 months ago


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4