Skip to content

ChatGPT + Python for Web Scraping: What Works (and What Doesn’t)

ChatGPT generates scraping code in seconds - but can it handle encoding errors, anti-bot blocks, and changing HTML? Here's what actually works in production.

10 min readIntermediate

Two Paths: Code Generator vs. Runtime Parser

You want to scrape 500 product listings. ChatGPT gives you two options.

Option 1: Ask ChatGPT to write a Python script. Copy it. Run it once. You now have a scraper you control – BeautifulSoup + CSS selectors, zero ongoing cost. The script is yours.

Option 2: Use the OpenAI API to parse each page at runtime. Send HTML to GPT, get back structured JSON. No selectors to maintain, but you’re paying $2-8 per million tokens. At 10K pages, that’s potentially $20-80 in API fees (according to April 2026 pricing from CostBench).

Which is better?

Option 1 is faster to prototype and free to scale – until the website changes its HTML. Option 2 adapts to layout changes automatically but costs real money per scrape. Most tutorials conflate these two completely different approaches. This one won’t.

When ChatGPT Code Generation Actually Works

Here’s the scenario where ChatGPT shines: you’re scraping a static site, you need data once or infrequently, and you’d rather not spend an hour writing BeautifulSoup selectors from scratch.

A junior analyst needs pricing from 20 competitor product pages. She’s never written a scraper. ChatGPT generates working code in 90 seconds.

The workflow is simple. Inspect the target page (right-click → Inspect), identify the HTML tags around your data, and tell ChatGPT: “Write a Python script using requests and BeautifulSoup to scrape [specific fields] from [URL]. Extract [CSS selector details].”

ChatGPT can’t scrape directly – it’s a text model, not a browser. What it does is generate Python code that you copy and run locally. Per the Clay blog, this is a critical distinction most beginners miss.

You’ll get a script that imports requests (for fetching HTML) and BeautifulSoup (for parsing). These are the two libraries every tutorial mentions because they’re simple and ChatGPT knows them well.

Pro tip: Always ask ChatGPT to add error handling for missing elements and to specify encoding='utf-8' in the response parsing. The encoding step prevents garbled text on non-English sites – an issue Oxylabs’ documentation highlights but most tutorials skip.

The Three-Step Setup (No Prior Coding Required)

Install Python 3.8 or higher. You can verify your version by running python --version in your terminal.

Next, install the two libraries ChatGPT will use:

pip install requests beautifulsoup4

That’s it. You’re ready.

Now open ChatGPT and paste a prompt like this:

Write a Python script using requests and BeautifulSoup to scrape product names and prices from https://example.com/products. The product name is in an <h2> tag with class "product-title" and the price is in a <span> with class "price". Save results to a CSV file.

ChatGPT returns code. Copy it into a file called scraper.py. Run it:

python scraper.py

If it works, you’re done. If it doesn’t, copy the error message back into ChatGPT and ask it to fix the code. This iterative debugging loop is where ChatGPT saves the most time for beginners.

But there’s a catch most tutorials don’t mention.

The Selector Trap Nobody Warns You About

ChatGPT generates CSS selectors based on the HTML you showed it. If you inspected Product A and later scrape Product B from the same site, the selector might break.

Why? Websites often use different HTML structures for different product categories, sale items, or out-of-stock listings. ChatGPT can’t verify this – it doesn’t browse the web, and per ScraperAPI’s guide, it can’t detect structure changes or test selectors across multiple pages.

The workaround: manually inspect 3-5 sample pages from different categories before trusting the selector. If the class names or tag structure vary, you’ll need to either generalize the selector or handle multiple patterns in your code.

This is where experienced developers have an edge. They know to check. Beginners don’t, and their scrapers fail silently on 30% of pages.

LLM Parsing: When to Pay for Intelligence

Now let’s talk about the other approach – using the OpenAI API at runtime to parse HTML.

Instead of writing selectors, you send raw HTML to GPT and ask it to extract structured data. According to Bright Data’s tutorial, this eliminates brittle CSS selectors and adapts to layout changes automatically.

The code looks like this:

import openai
import requests
from bs4 import BeautifulSoup

response = requests.get('https://example.com/product')
html = response.text

result = openai.ChatCompletion.create(
 model="gpt-4.1",
 messages=[{"role": "user", "content": f"Extract product name, price, and rating from this HTML: {html}"}]
)

print(result['choices'][0]['message']['content'])

This works beautifully for variable-structure pages – think Airbnb listings where some have pools, some don’t, and the HTML differs wildly.

But the cost adds up fast. GPT-4.1 costs $2.00 input / $8.00 output per million tokens (verified April 2026 via CostBench). A typical product page is 5,000-10,000 tokens. Scraping 10,000 pages means 50-100 million input tokens plus output – potentially $100-200 in API fees.

A Medium analysis found that typical e-commerce scraping that cost $2,000/month with LLM parsing dropped to under $50/month using traditional selectors. The 40x cost difference is real.

When does it make sense? When the data is unstructured, the site changes layout frequently, or you’re scraping a small dataset (under 1,000 pages) where accuracy matters more than cost.

What Breaks at Scale (And How to Know Before You Hit It)

ChatGPT-generated scrapers work fine for 10 pages. At 1,000 pages, three things break:

1. Anti-bot blocks. Sites use CAPTCHAs, IP rate limiting, and browser fingerprinting to detect scrapers. A basic requests call looks nothing like a real browser. According to Oxylabs, most production sites block these requests within minutes.

The fix requires rotating proxies, user-agent headers, and sometimes headless browsers like Playwright. ChatGPT can generate that code, but you’re now maintaining a 200-line script instead of a 20-line one.

2. Encoding errors. ChatGPT often forgets to specify encoding='utf-8' when handling responses. On English-only sites, this works fine. On sites with accented characters, Chinese, or Arabic, you get garbage text or a UnicodeDecodeError. Oxylabs’ tutorial mentions this, but it’s buried in a note most readers skip.

3. Hallucinated fixes. When you paste an error into ChatGPT, it confidently suggests a solution. But as the hallucination problem documented in the Crawlbase guide shows, GPT sometimes blames the wrong thing – it might say your selector is incorrect when the real issue is rate limiting or a CAPTCHA.

You need to verify the diagnosis before applying the fix. Beginners trust the AI blindly and waste hours.

Production Readiness Checklist

  • Does your scraper handle missing elements gracefully (try/except blocks)?
  • Did you specify encoding='utf-8' in response handling?
  • Have you tested the selectors on 5+ sample pages, including edge cases (out of stock, sale items)?
  • Does your code include delays between requests (time.sleep(1)) to avoid rate limits?
  • If using the OpenAI API for parsing, have you calculated cost at full scale?

If any answer is no, your scraper isn’t production-ready.

The Tools ChatGPT Won’t Suggest (But You Might Need)

ChatGPT defaults to BeautifulSoup because it’s simple. But for JavaScript-heavy sites – anything with infinite scroll, dynamic loading, or React-based UIs – BeautifulSoup sees empty divs.

Playwright is the better tool here. According to the Apify tutorial, it’s one of the best frameworks for scraping dynamic websites. It actually runs a browser, executes JavaScript, and waits for content to load.

You can ask ChatGPT to generate Playwright code instead:

"Write a Python script using Playwright to scrape product names from https://example.com. Wait for the page to load fully before extracting data."

But Playwright is slower and heavier than requests. Use it only when BeautifulSoup fails.

Another tool ChatGPT rarely mentions: the Batch API. If you’re scraping data for offline analysis (not real-time), OpenAI’s Batch API cuts LLM parsing costs by 50%. Per official pricing docs, GPT-4.1 drops from $2/$8 to $1/$4 per million tokens. Results arrive within 24 hours.

For nightly competitor pricing scrapes or weekly content audits, this makes LLM parsing economically viable.

Tool When to Use Cost
BeautifulSoup + requests Static HTML, simple structure Free
Playwright JavaScript-heavy sites Free (but slower)
OpenAI API (realtime) Unstructured data, under 1K pages $2-8 per 1M tokens
OpenAI Batch API Offline analysis, 24hr turnaround OK $1-4 per 1M tokens (50% off)

Free vs. Paid ChatGPT: Does It Matter?

Yes, but not how you’d think.

Free ChatGPT uses GPT-3.5 or GPT-4o-mini. Paid ChatGPT Plus ($20/month) uses GPT-4 or GPT-4o. According to the ScraperAPI guide, the paid version generates more accurate code, especially for current library versions and complex selectors.

The free version sometimes suggests outdated methods – older Beautiful Soup syntax, deprecated library functions, or inefficient logic. If you’re learning, the free tier is fine. If you’re building something that needs to work tomorrow without debugging, Plus is worth it.

But here’s what both versions struggle with: they can’t test the code, can’t verify selectors work across pages, and can’t detect anti-bot measures. Those limitations don’t change with the paid tier.

When to Stop Using ChatGPT

ChatGPT gets you from zero to working scraper fast. But there’s a point where it becomes a liability.

If you’re scraping the same site daily, maintaining the scraper yourself is faster than re-prompting ChatGPT every time something breaks. Learn the selector syntax. Understand the request flow. Own the code.

If you’re scraping at scale (10K+ pages), invest in proper infrastructure – rotating proxies, retry logic, monitoring. ChatGPT won’t build that for you in one prompt.

If data accuracy is critical (financial data, legal documents), verify every extraction manually. LLMs can hallucinate. Selectors can miss edge cases. Trust, but verify.

The right mental model: ChatGPT is a junior developer who writes code fast but doesn’t test it. You’re the senior developer who reviews, tests, and deploys. Act accordingly.

FAQ

Can ChatGPT scrape websites directly, or do I need to run code myself?

ChatGPT cannot scrape websites. It generates Python code that you copy and execute on your own machine. The code uses libraries like BeautifulSoup and requests to fetch and parse HTML. If you want ChatGPT to access live URLs, you’d need to use the OpenAI API programmatically and send page content to it – but that’s a different workflow entirely, and it costs money per request.

What’s the difference between using ChatGPT to write scraping code vs. using the OpenAI API to parse HTML?

ChatGPT code generation is a one-time interaction – you get a script, run it as many times as you want, no ongoing cost. OpenAI API parsing sends each page’s HTML to GPT at runtime and pays per token ($2-8 per million tokens for GPT-4.1 as of April 2026). The first approach is cheaper and faster but requires you to maintain selectors. The second adapts to layout changes but can cost $20-80 for 10K pages. Choose based on whether you value cost or adaptability.

Why does my ChatGPT-generated scraper work on some pages but fail on others from the same site?

Websites often use different HTML structures for different product types, categories, or item states (in stock vs. out of stock). ChatGPT generates selectors based on the one page you showed it, and it can’t verify structure across the entire site. Before trusting a scraper, manually inspect 3-5 sample pages from different sections. If the HTML varies, you’ll need to generalize the selector or add conditional logic to handle multiple patterns. This is the most common silent failure mode for beginners.

Next step: pick a simple site, write your prompt, and run the code. If it breaks, you now know the three places to look first – encoding, selectors, or rate limits. That’s more than most tutorials will tell you.

For a deeper dive into OpenAI API integration, see the official pricing documentation and Bright Data’s LLM scraping tutorial.