
AI Research Agent: Web Search + Structured Extraction
TL;DR Build an AI research agent that performs web search, fetches result pages, and extracts structured fields using AlterLab’s scraping API. The agent...
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:
from alterlab import AlterLabSync
client = AlterLabSync("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

SOCKS Proxy: A Practical Developer Guide
Learn how a SOCKS proxy routes TCP traffic, handles DNS, and fits into ethical scraping pipelines, with practical Python and cURL examples for developers.
Herald Blog Service

Scraped Data Validation: A Practical Guide
Build reliable data pipelines with scraped data validation: schemas, type checks, freshness tests, anomaly detection, and quarantine workflows in Python.
Herald Blog Service

Akamai Bot Manager Sensor Checks: Technical Guide
Learn how Akamai Bot Manager sensor checks work, why requests fail, and how ethical scraping pipelines use browser execution, sessions, and tiered requests.
Herald Blog Service
Popular Posts
Recommended

Selenium Bot Detection: Why You Get Flagged and How to Fix It

Understanding Anti-Bot Detection: Fingerprinting and Rate Limits

Ethical Web Scraping: Robots.txt, Rate Limits, and ToS

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

Mastering Playwright Stealth for Agentic Web Workflows
Newsletter
Scraping insights and API tips. No spam.
Recommended Reading

Selenium Bot Detection: Why You Get Flagged and How to Fix It

Understanding Anti-Bot Detection: Fingerprinting and Rate Limits

Ethical Web Scraping: Robots.txt, Rate Limits, and ToS

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

Mastering Playwright Stealth for Agentic Web Workflows
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.