Web scraping for machine learning - how to build quality training datasets - DataImpulse

A machine-learning model is only as good as the data it learns from, and for most teams that data comes from the web. Machine learning data collection — gathering, cleaning, and structuring real-world examples into a training dataset — is where a lot of model quality is won or lost. This guide walks through web scraping for machine learning end to end — how to build a quality training dataset: what “quality” means, the step-by-step process, the cleaning that matters most, the legal line, and why collection at scale runs on proxies.

I’m Andrii Byzov, an AI-Native Fractional CMO who builds web-data pipelines for ML and analytics. Below: the dataset qualities that move model performance, a practical scraping-to-dataset workflow with code, and how residential proxies keep collection scalable and diverse.


Key Facts

  • Quality, dedup, and label accuracy often matter more than raw volume. A clean, diverse, well-labeled dataset can train a better model than a bigger noisy one — though scale still helps once quality is controlled.
  • The web is a common source for many ML datasets — text, images, prices, reviews, listings — collected by scraping where no API, licensed feed, or open dataset fits.
  • For geo-sensitive tasks, use region-aware collection. When language, pricing, or availability varies by location, pulling examples from many regions (via geo-targeted proxies) avoids a skewed dataset.
  • High-volume scraping of rate-limited sites often uses proxies. A single IP gets cut off, so large collections lean on rotating residential IPs — but proxies don’t override site terms.
  • How you obtained the data matters legally. In Bartz v. Anthropic (2025) the court held training on lawfully acquired books was fair use on those facts, but rejected fair use for building a library from pirated copies — provenance is part of compliance.

What Makes a Good Training Dataset?

Before scraping anything, know what you’re aiming for. A quality training dataset has five traits:

  • Relevance — the examples match the task and the distribution your model will see in production.
  • Diversity — varied sources, regions, and edge cases, so the model generalizes instead of memorizing one slice.
  • Clean labels — accurate, consistent annotations; label noise caps your achievable accuracy.
  • Balance — classes and segments represented in sensible proportions, not dominated by whatever was easiest to scrape.
  • Freshness — recent enough that the patterns still hold, with a plan to refresh as the world drifts.

Most “more data didn’t help” problems trace back to one of these being weak — usually duplication, label noise, or a dataset skewed to one source. This is the angle this guide focuses on; for the proxy-buying view see best proxies for ML training, and for pitfalls see mistakes teams repeat collecting AI training data.


How to Build a Training Dataset by Web Scraping

Step 1. Define the schema and labels. Decide the exact fields and label set before collecting — what each example contains and how it’s tagged. Working backward from the model’s task prevents collecting data you can’t use.

Step 2. Source and scrape at scale. Identify the sites holding your examples and scrape them, routed through proxies for volume and regional diversity. Tag every example with its source and label as you collect.



import requests
from bs4 import BeautifulSoup

HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                         "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"}
# Rotating residential proxies: scale collection without IP blocks, and pull
# region-diverse examples so the dataset isn't skewed to one location.
proxies = {"http":  "http://LOGIN__cr.us;sid.ml1:[email protected]:823",
           "https": "http://LOGIN__cr.us;sid.ml1:[email protected]:823"}

def collect(urls, label):
    """Scrape raw text examples and tag each with its source + label."""
    rows = []
    for url in urls:
        try:
            r = requests.get(url, headers=HEADERS, proxies=proxies, timeout=30)
            r.raise_for_status()
        except requests.RequestException:
            continue
        text = BeautifulSoup(r.text, "html.parser").get_text(" ", strip=True)
        rows.append({"text": text, "label": label, "source": url})
    return rows






















Step 3. Clean and deduplicate. Raw scraped data is noisy and full of duplicates — and duplicates are the fastest route to a biased, overfit model. Drop empties and too-short samples, normalize text, and remove exact (and near-) duplicates before anything else.



import hashlib, re

def clean(t):
    return re.sub(r"\s+", " ", t).strip().lower()

def dedup(rows, min_len=40):
    """Drop empties, too-short samples, and exact duplicates — noisy,
    duplicated data is the fastest way to a biased, overfit model."""
    seen, out = set(), []
    for row in rows:
        text = row["text"]
        if not text or len(text) < min_len:
            continue
        key = hashlib.sha1(clean(text).encode()).hexdigest()
        if key in seen:
            continue
        seen.add(key)
        out.append(row)
    return out

urls = ["https://example.com/a", "https://example.com/b"]   # your source list
dataset = dedup(collect(urls, label="positive"))
print(f"{len(dataset)} deduplicated examples")























Step 4. Label and structure. Apply consistent labels — programmatically where you can (weak supervision, heuristics), with human review on a sample to measure label quality. Store the dataset in a structured, versioned format (JSONL/Parquet) so you can reproduce training runs.

Step 5. Validate quality. Check class balance, source distribution, and obvious bias before training. A quick audit — how many examples per class, per region, per source — catches the skew that quietly wrecks model performance.


Why Proxies Matter for ML Data Collection

Two problems make machine learning data collection at scale fail without proxies. Volume: training datasets need thousands to millions of examples, and sites rate-limit and block aggressive crawling — a single IP gets cut off fast. Diversity: if every example comes from one location, the dataset inherits that bias, so you want examples pulled from many regions. DataImpulse residential proxies ($1/GB, rotating, 195 countries) help with both — a large rotating pool sustains volume, and geo-targeting pulls region-diverse examples for geo-sensitive tasks — though proxies help you collect, they don’t override a site’s terms. For the wider picture on web-collected data, see our guide on alternative data.

Is It Legal to Scrape Data for ML Training?

Collection and training are two separate legal questions. Collection: the usual rules apply — public, non-personal data is generally lower-risk, but it still depends on copyright, site terms, access controls, and privacy law; avoid login-gated content, personal data, and bypassing access controls. Training: in Bartz v. Anthropic (2025) a US court held training on lawfully acquired books was fair use on those facts, but rejected fair use for building a library from pirated copies — so how you obtained the data matters as much as what you do with it. Keep provenance clean, respect robots.txt and site terms, and get counsel for a commercial model. For the full framework, see whether web scraping is legal. This is general information, not legal advice.


Frequently Asked Questions

What is machine learning data collection?

It’s the process of gathering, cleaning, and structuring real-world examples into a dataset a model can train on. For most teams the main source is the public web — text, images, prices, reviews, listings — collected by scraping where no API or licensed feed exists, then deduplicated, labeled, and validated.

How much data do I need to train a model?

It depends on the task and model, but quality and diversity matter more than raw count. A smaller, clean, well-balanced, well-labeled dataset usually beats a larger noisy one. Focus on removing duplicates and label noise and covering the cases your model will see in production before chasing volume.

Why is deduplication so important for training data?

Duplicates over-weight whatever is repeated, biasing the model and inflating evaluation scores (the same example can land in both train and test sets). Removing exact and near-duplicates is one of the highest-impact cleaning steps — it improves generalization and makes your metrics trustworthy.

Do I need proxies to scrape ML training data?

For high-volume scraping of sites that rate-limit by IP, usually — building a dataset means many requests, and a single IP gets cut off fast. Rotating residential proxies sustain volume and let you pull region-diverse examples — though they don’t override a site’s terms, so collect responsibly. (APIs, licensed feeds, or open datasets like Common Crawl can sometimes avoid scraping entirely.)

Is it legal to scrape data to train a model?

Collection and training are separate questions. Collecting public, non-personal data is generally lower-risk (not automatically clear — copyright, ToS, access controls, and privacy law still apply); avoid logins, personal data, and bypassing controls. For training, Bartz v. Anthropic (2025) held training on lawfully acquired books fair use on those facts but rejected it for a pirated library — provenance matters. Respect site terms and get counsel for commercial use. Not legal advice.


Conclusion

Building a quality training dataset is a process, not a download: define the schema, scrape the right sources at scale, deduplicate and clean ruthlessly, label consistently, and validate for balance and bias. The web is where most of the data lives, so the collection layer — geo-diverse, high-volume, unblocked — is what makes the rest possible, and that’s what residential proxies provide. Keep provenance clean and the legal line in view, and your machine learning data collection produces datasets a model can actually learn from. For the infrastructure, see best proxies for ML training and best proxies for web scraping.

Last updated: June 25, 2026.



Share article: