A RetroSearch Logo

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

Search Query:

Showing content from https://docs.agentql.com/python-sdk/api-references/agentql-tools below:

Website Navigation


AgentQL Tools | AgentQL

The agentql.tools module provides utility methods to help with data extraction and web automation.

Paginate

The following example demonstrates how to use the paginate method to collect data from multiple pages:

agentql_pagination_example.py

python

import agentql
from agentql.tools.sync_api import paginate 
from playwright.sync_api import sync_playwright

with sync_playwright() as p, p.chromium.launch(headless=False) as browser:
    page = agentql.wrap(browser.new_page())
    page.goto("https://news.ycombinator.com/")

    # Define the query to extract the titles of the posts
    QUERY = """
    {
        posts[] {
            title
        }
    }
    """

    # Collect data from the first 3 pages using the query
    paginated_data = paginate(page, QUERY, 3)
    print(paginated_data)
Methods Paginate

Collects data from multiple pages using an AgentQL query. Internally, the function first attempts to find the operable element to navigate to the next page and click it, then uses the provided query to extract the data from the page. The function then repeats this process for the specified number of pages.

note

The paginate function returns data collected from each page into a single, aggregated list. If you wish to step through each page's data, use the navigate_to_next_page method instead.

Usage paginate_example.py

python

paginated_data = paginate(page, QUERY, 3)
Arguments Returns Query document

The following example demonstrates how to use the query_document method to query a document (.pdf, .png, .jpg, .jpeg) using AgentQL.

note

The query_document function consumes 1 api call per image (JPG, JPEG, JPG), and 1 api call for each page within a PDF.

query_document_example.py

python

from agentql.tools.sync_api import query_document

QUERY = """
{
    name
}
"""

file_path = "path/to/file.pdf"

async def main():
    response = query_document(
        file_path,
        query=QUERY,
    )
    print(f"name: {response['name']}")

main()
Methods query_document Queries a document (.pdf, .png, .jpg, .jpeg) using AgentQL. Usage query_document_example.py

python

response = query_document(
        file_path,
        query=QUERY,
    )
Arguments Returns Types ResponseMode

The ResponseMode type specifies the mode of querying for query_elements(), query_data(), and get_by_prompt() methods. It's expecting the following two values:

Executes the query in Standard Mode. Use this mode when your queries are complex or extensive data retrieval is necessary.

Executes the query more quickly, potentially at the cost of response accuracy. This mode is useful in situations where speed is prioritized, and the query is straightforward.

Use remote browser

The following example demonstrates how to use the create_browser_session method to create a remote browser session:

remote_browser_example.py

python

import agentql
from agentql.tools.sync_api import create_browser_session, UserAgentPreset 
from playwright.sync_api import sync_playwright

session = create_browser_session(ua_preset=UserAgentPreset.WINDOWS)

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(session.cdp_url)
    page = agentql.wrap(browser.new_page())

    # Use AgentQL as normal
    page.goto("https://example.com")

    # Get streaming URL to view the page
    print(f"View at: {session.get_page_streaming_url(0)}")
Methods create_browser_session

Creates a new remote browser session that provides a Chrome DevTools Protocol (CDP) URL for connecting to a remote browser instance and streaming URLs for real-time page viewing.

note

Remote browser sessions allow you to run browser automation on remote infrastructure while maintaining the familiar AgentQL API. See the Remote Browser Guide for complete usage examples.

Usage create_browser_session_example.py

python

session = create_browser_session(ua_preset=UserAgentPreset.MACOS)
Arguments Returns Raises Types BrowserSession

Represents an allocated remote browser session with CDP access and streaming capabilities.

Properties Methods UserAgentPreset

Enum for user agent presets to simulate different operating systems:


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