Skip to main content

A browser for your agent, built on Chrome and Pyppeteer.

Project description

agentbrowser

A browser for your agent, built on Chrome and Pyppeteer.

Installation

pip install agentbrowser

Usage

Importing into your project

from agentbrowser import (
    get_browser,
    init_browser,
    navigate_to,
    get_body_html,
    get_body_text,
    get_body_text_raw,
    get_document_html,
    create_page,
    close_page,
    evaluate_javascript,
)

Basic:

Create a new page

Equivalent of ctrl+t in Chrome. Makes a new blank page.

page = create_page()

Close a page

Equivalent of ctrl+w in Chrome. Closes the current page.

close_page(page)

Navigate to a URL

Equivalent of typing a URL into the address bar and hitting enter. If you haven't created a page yet, it will create one for you.

page = navigate_to("https://google.com")

Get the HTML of the page

Get the entire document HTML

html = get_document_html(page)

Get the HTML of the body

Get just the HTML of the body and inner. Useful for parsing out the content of the page.

html = get_body_html(page)

Get the text of the body

Get just the text of the body. Unlike the raw function, tries to remove some useless tags and divs and things. Not perfect, though.

text = get_body_text(page)

Get the raw text of the body

Get the raw text of the body. This will include all the tags and divs and things.

text = get_body_text_raw(page)

Advanced Usage

Get browser

This will give you a reference to the browser object, which you can use for advanced stuff. The browser object comes from Pyppeteer, so anything you can do with Pyppeteer, you can do with this.

browser = get_browser()

Evaluate Javascript

Call some Javascript on the page. Equivalent of opening the console and typing in some Javascript.

result = evaluate_javascript(page, "document.title")

Initialize browser

This will initialize the browser object. You can pass headless and executable_path. Headless will control whether the actual window appears on screen. Executable path will control which browser is used. By default, it will try to find Chrome first, then fall back to Chromium if it can't find Chrome.

The browser will be auto-initialized by default so you don't need to call this. The only reason you would is because you want to use headful or swap the browser.

init_browser(headless=True, executable_path="/path/to/chrome")
bash publish.sh --version=<version> --username=<pypi_username> --password=<pypi_password>

Contributions Welcome

If you like this library and want to contribute in any way, please feel free to submit a PR and I will review it. Please note that the goal here is simplicity and accesibility, using common language and few dependencies.

Questions, Comments, Concerns

If you have any questions, please feel free to reach out to me on Twitter or Discord.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentbrowser-0.1.1.tar.gz (5.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page