Passing Context to Queries
Overview
AgentQL supports contextual queries, enhancing the precision of your results. Context allows you to specify additional details about the data you want to extract in plain English. This guide demonstrates how to incorporate context into your queries and leverage it for improved accuracy.
Adding context to queries
Add context to queries in parentheses ()
after the term. For example, if you want to scrape all the products on the page, but exclude sponsored results, you can add the context (exclude sponsored results)
to the products
term.
Example: Excluding sponsored results
With Context:
Without Context:
Using context to select the correct element
Context can be particularly useful when you need to select the correct element from multiple similar elements on a page. Here is a real world example where context distinguishes between different URLs to scrape in a Reddit post.
In this example, the context "page URL, not the link inside the post" helps AgentQL focus on the correct browser URL instead of the link inside the post.
Select specific HTML properties
You can also add context to select specific HTML properties. Occasionally AgentQL may return the wrong element. In this case, you can add context to ensure the specific HTML properties you want. Here are some examples:
Without Context:
With Context:
With Specific Class Context:
Conclusion
By incorporating contextual information into your queries, AgentQL offers a powerful way to refine and enhance your data extraction process. This approach not only improves accuracy but also provides flexibility in handling complex web structures and specific data requirements. As you become more familiar with contextual queries, you'll find them invaluable for tackling a wide range of web scraping challenges efficiently.