Skip to main content

Build an AI Web Scraper with Python in 2026

Published 3 min readMarkdown

Most web scrapers break the moment a site changes its HTML structure. In 2026, the fix is simple: let AI parse the page for you instead of hardcoding selectors.

Here's how to build a scraper that uses an LLM to extract structured data from any page — no CSS selectors, no XPath.


The problem with classic scrapers

# This breaks every time the site redesigns
price = soup.find("span", class_="price-box__final-price").text

One class name change and your scraper is dead.


The AI approach

Instead of targeting specific elements, you send the raw HTML to an LLM and ask it to extract what you need. The LLM understands context — it finds the price whether it's in a <span>, a <div>, or a custom component.


Setup

pip install requests beautifulsoup4 anthropic

The scraper

import requests
from bs4 import BeautifulSoup
import anthropic

client = anthropic.Anthropic(api_key="your_api_key")

def ai_scrape(url: str, extract: str) -> str:
    # Fetch the page
    headers = {"User-Agent": "Mozilla/5.0"}
    html = requests.get(url, headers=headers).text

    # Clean the HTML — keep only visible text
    soup = BeautifulSoup(html, "html.parser")
    for tag in soup(["script", "style", "nav", "footer"]):
        tag.decompose()
    clean_text = soup.get_text(separator="\n", strip=True)[:8000]

    # Ask the LLM to extract the data
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=512,
        messages=[{
            "role": "user",
            "content": f"Extract the following from this webpage content: {extract}\n\n{clean_text}"
        }]
    )
    return message.content[0].text

# Example usage
result = ai_scrape(
    url="https://news.ycombinator.com",
    extract="The top 5 article titles and their links"
)
print(result)

Output example

1. Show HN: I built a Python library to detect antibots before scraping - https://...
2. GPT-5 released with 1M context window - https://...
3. Why Rust is overtaking Go in 2026 - https://...
4. The death of REST APIs - https://...
5. How I scraped 10M records without getting blocked - https://...

No selector. No XPath. Just plain English.


When to use this

SituationUse
Simple static siteClassic BeautifulSoup
Site that changes oftenAI scraper
Need structured JSON outputAI scraper
High volume (1000+ pages)Classic (cheaper)

Extract structured JSON

Ask the LLM to return JSON directly:

result = ai_scrape(
    url="https://example-shop.com/product/chair",
    extract="Return a JSON object with: name, price, currency, in_stock (boolean)"
)

import json
data = json.loads(result)
print(data)
# {"name": "Ergonomic Chair", "price": 299.99, "currency": "USD", "in_stock": true}

Going further

This approach works but has limits at scale — LLM calls cost money and are slower than direct parsing.

For production scraping pipelines, anti-bot detection, and high-volume extraction, check out codes-me.com — we build custom scraping solutions tailored to your needs.

Also check out scrapalyser — our open-source Python library that detects antibots, JS requirements, and login walls before you scrape:

pip install scrapalyser

GitHub: github.com/codesme34/scrapalyser

About the author

No comments yet. Be the first to comment.