
AI Research Agent: Web Search + Structured Extraction
Learn how to build an AI research agent that combines web search with AlterLab's scraping API to extract structured data from public web pages, using Python SDK and cron scheduling.
AlterLab handles this automatically — scrape any URL with one API call. No infrastructure required.
Try it freeTL;DR
Build an AI research agent that performs web search, fetches result pages, and extracts structured fields using AlterLab’s scraping API. The agent uses the Python SDK for HTTP requests and AlterLab’s anti‑bot handling to retrieve clean data, then parses it with a lightweight HTML parser. Schedule the script with cron to run unattended.
Introduction
AI research agents reduce manual effort in data collection by automating two core steps: discovering relevant pages and pulling out specific information. Instead of copying‑pasting from search results, the agent issues a search query, retrieves the top URLs, scrapes each page, and extracts fields such as title, price, or metadata into a structured format. This guide shows how to implement that flow with AlterLab’s API, which manages proxies, browser rendering, and anti‑bot challenges so you can focus on the extraction logic.
Architecture Overview
The agent consists of four components:
- Query Builder – turns a natural language question into a search engine URL.
- Search Fetcher – retrieves the search results page and extracts links.
- Page Scraper – fetches each target page via AlterLab and returns raw HTML.
- Data Parser – runs a selector‑based or regex‑based extractor to produce JSON records.
Each component can be swapped or scaled independently. For example, you could replace the search fetcher with a dedicated search API, or add a deduplication step before scraping.
Setting Up AlterLab
First, create an account at AlterLab and generate an API key. Install the official Python SDK to simplify requests:
pip install alterlabThe SDK handles authentication, retries, and response parsing. Initialize it with your key:
import alterlab
client = alterlab.Client("YOUR_API_KEY") # authenticates all requestsWhen you call client.scrape, AlterLab automatically routes the request through a headless browser if needed, applies rotating proxies, and solves common anti‑bot challenges. This means you can scrape sites that employ basic bot detection without managing your own browser fleet. For details on the underlying technology, see the anti‑bot handling page.
Implementing Web Search
We’ll use a public search endpoint that returns HTML results (e.g., a search engine’s results page). The goal is to pull the first N links. The following function builds a search URL, fetches the page, and extracts anchors with a simple CSS selector.
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
def fetch_search_links(query: str, limit: int = 5) -> list[str]:
"""Return a list of result URLs for the given query."""
encoded = quote_plus(query)
search_url = f"https://example.com/search?q={encoded}" # generic placeholder
resp = client.scrape(search_url, formats=["html"])
soup = BeautifulSoup(resp.text, "html.parser")
links = []
for a in soup.select("a.result-link"): # adjust selector to your target
href = a.get("href")
if href and href.startswith("http"):
links.append(href)
if len(links) >= limit:
break
return linksKey points:
- The request uses
formats=["html"]to get raw HTML; you could also ask for["text"]if you only need visible text. - The selector
"a.result-link"is a placeholder; replace it with the appropriate selector for the search engine you target. - The function returns a list of absolute URLs ready for scraping.
Try it yourself – replace the placeholder URL with a real search endpoint and run the function with a test query to see the links it returns.
Try scraping a search results page with AlterLab
Structured Extraction with AlterLab
Once you have a list of URLs, the next step is to scrape each page and pull out the fields you need. AlterLab can return data in multiple formats; requesting JSON often saves a parsing step if the site offers structured data via JSON‑LD or microdata. Otherwise, you receive HTML and parse it yourself.
The example below scrapes a product‑like page and extracts title, price, and availability using BeautifulSoup. Adjust the selectors to match the structure of your target pages.
def scrape_and_extract(url: str) -> dict:
"""Scrape aWas this article helpful?
Frequently Asked Questions
Related Articles

Grounding LLM Responses with Live Web Data: Patterns and Pitfalls
Learn how to safely feed real-time web data into LLMs, avoid hallucinations, and implement reliable grounding pipelines using AlterLab's scraping API.
Herald Blog Service

Automating Competitive Intelligence with Web Data APIs
Learn how to automate competitive intelligence pipelines using web data APIs and LLM summarization to extract, process, and summarize market data at scale.
Herald Blog Service

Web Search API for AI Agents: Developer's Guide
Learn how to build a robust web search API for AI agents using RAG, headless browsers, and anti-bot handling to ensure reliable real-time data extraction.
Herald Blog Service
Popular Posts
Recommended
Newsletter
Scraping insights and API tips. No spam.
Recommended Reading

How to Scrape AliExpress: Complete Guide for 2026

Why Your Headless Browser Gets Detected (and How to Fix It)

AlterLab vs Firecrawl: Which Scraping API Is Better in 2026?

How to Scrape Twitter/X Data: Complete Guide for 2026

How to Scrape Cloudflare-Protected Sites in 2026
Stay in the Loop
Get scraping insights, API tips, and platform updates. No spam — we only send when we have something worth reading.
Explore AlterLab
Web Scraping API Resources
Part of the Web Scraping API Documentation cluster
Complete API reference with 5-tier auto-escalation — Curl to challenge resolution.
Pillar pageConfigure Tier 4 browser rendering for SPAs and dynamic content.
Scrape pages behind login using session management.
Real success rates and cost data across all 5 tiers.
MCP Server, Python SDK, and Firecrawl-compatible API for AI agent workflows.