How to use AgentQL?

To use AgentQL, follow these steps:

  1. Installation:

    • Install the AgentQL library: pip install agentql
    • Install Playwright dependencies: playwright install
    • Set your AgentQL API key as an environment variable: AGENTQL_API_KEY[2]
  2. Import required libraries:

    from agentql import wrap, configure
    from playwright.sync_api import sync_playwright
    
  3. Configure AgentQL with your API key:

    configure(api_key=your_api_key)
    
  4. Launch a browser and navigate to a website:

    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = wrap(browser.new_page())
        page.goto("https://example.com")
    
  5. Define an AgentQL query to interact with elements:

    QUERY = """
    search_box
    """
    
  6. Execute the query and interact with elements:

    response = page.query_elements(QUERY)
    search_box = response.search_box
    search_box.fill("search term")
    
  7. For data extraction, use the query_data method:

    data = page.query_data(QUERY)
    

AgentQL offers natural language selectors, making it intuitive to find elements based on their context and meaning rather than DOM structure[5]. It can be used for web scraping, automation, and testing across different websites[4].

Citations:
[1] https://docs.agentql.com/quick-start
[2] https://docs.agentql.com/python-sdk/installation
[3] https://docs.agentql.com/getting-started/first-steps
[4] https://github.com/tinyfish-io/agentql
[5] https://docs.agentql.com/concepts/query-language
[6] https://www.agentql.com/scraping/jetbrains-com
[7] https://docs.agentql.com/guides
[8] https://docs.agentql.com/concepts/under-the-hood