Web scraping is using software to collect information from web pages automatically. A scraper fetches a page, parses its HTML, pulls out the specific fields you care about, such as a price or a product name, and stores them in a spreadsheet or database. It is the same thing you would do by copying and pasting, done by a program, at a steady pace, every day.
How does web scraping work, step by step?
Almost every scraper, small or large, follows the same four steps.
- Fetch. The scraper sends an HTTP request for a page, exactly as a browser does, and gets back the response: usually HTML.
- Parse. It turns that raw HTML into a tree of elements it can search, so it can find "the heading", "the third table" or "every element with the class
price". - Extract. It picks out the fields you want and cleans them. A price like "₹1,299.00" becomes a clean value like 1299.00. Dates get a single format. Blank values are flagged.
- Store. It writes the clean records somewhere useful: a CSV file, a database table or a spreadsheet, often with the time it was collected and the source URL.
Around those four steps sit two more jobs. A scheduler decides when to run again, and a monitor tells you when something breaks, because websites change their layouts without warning.
What does a basic scraper look like?
Here is a small Python example. It checks robots.txt first, fetches one page, parses it and extracts product names and prices. It uses the requests and beautifulsoup4 libraries.
import csv
import time
from urllib import robotparser
import requests
from bs4 import BeautifulSoup
BASE = "https://example.com"
URL = f"{BASE}/products"
AGENT = "AcmePriceBot/1.0 (+https://acme.example/bot)"
# 1. Respect robots.txt before fetching anything
rp = robotparser.RobotFileParser(f"{BASE}/robots.txt")
rp.read()
if not rp.can_fetch(AGENT, URL):
raise SystemExit("robots.txt disallows this path")
# 2. Fetch, identifying who you are
resp = requests.get(URL, headers={"User-Agent": AGENT}, timeout=30)
resp.raise_for_status()
# 3. Parse and extract
soup = BeautifulSoup(resp.text, "html.parser")
rows = []
for card in soup.select(".product"):
name = card.select_one(".name").get_text(strip=True)
price = card.select_one(".price").get_text(strip=True)
rows.append({"name": name, "price": price.replace("₹", "").replace(",", "")})
# 4. Store
with open("products.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["name", "price"])
writer.writeheader()
writer.writerows(rows)
time.sleep(5) # pause before the next page if you loop over more
The CSS selectors (.product, .name, .price) are the fragile part. They depend on the site's HTML, and when the site redesigns, they stop matching. That is why monitoring matters more than clever code.
Static pages vs JavaScript-rendered pages
Not every page sends its data in the HTML.
| Static (server-rendered) page | JavaScript-rendered page | |
|---|---|---|
| What the first response contains | The full content | A mostly empty shell plus scripts |
| How to fetch it | A plain HTTP request | A headless browser, or the page's own data requests |
| Speed and cost | Fast and cheap | Slower, uses much more CPU and memory |
| Typical tools | requests, httpx, Beautiful Soup | Playwright, Puppeteer |
A quick test: open the page, choose "View page source" and search for the text you want. If it is there, a plain request will work. If it is not, the browser built it with JavaScript after loading.
For JavaScript pages, check your browser's developer tools first. Open the Network tab and reload. Often the page fetches its data as JSON from an endpoint, and reading that is cleaner than rendering the whole page, provided the site's terms allow it. When you genuinely need a real browser, a tool like Playwright drives one for you. Running many browsers reliably is its own problem, covered in our post on scaling Playwright scrapers.
Why should you look for an API first?
Before you write a scraper, check whether the site offers an official API, a data export or a feed. Many do, especially marketplaces, payment providers and government data portals.
An API has real advantages:
- It is stable. The fields are documented and change with notice, not with every redesign.
- It is permitted. You are using the data the way the owner intended, usually under clear terms.
- It is cheaper to run. Structured JSON needs no parsing of layout, and no browser.
Scrape only when there is no API, or it lacks what you need, and the data is public and allowed to be collected.
What is robots.txt and do you have to follow it?
robots.txt is a plain text file at the root of a site (for example example.com/robots.txt) that tells automated clients which paths they may and may not crawl. The format was standardised in RFC 9309, published by the IETF in September 2022.
A few things the RFC makes clear:
- It is not a lock. The RFC says the rules "are not a form of access authorization." A site cannot use robots.txt to secure anything, and you should not treat "not disallowed" as "permitted for any use".
- Refresh it. Crawlers should not use a cached copy for more than 24 hours, unless the file cannot be reached.
- Errors matter. If robots.txt returns a 4xx error, a crawler may treat it as absent. If it returns a 5xx server error, the crawler must assume everything is disallowed.
- Crawl-delay is not in the standard. Some sites still include it. Honouring it is the polite choice.
Following robots.txt is the baseline of good behaviour, and ignoring it will be held against you in any dispute.
How fast should a scraper go?
Slower than you think. A burst of traffic from one scraper can slow a small site down for its real customers.
Good habits:
- Pace your requests. Leave a pause between requests to the same site, and run one or a few at a time, not hundreds.
- Back off when asked. HTTP status 429, defined in RFC 6585, means "too many requests". It may include a
Retry-Afterheader that says how long to wait. Wait at least that long, and slow down for the rest of the run. - Identify yourself. Use a clear User-Agent with a way to contact you, as in the example above.
- Do not re-fetch what has not changed. Cache pages and only collect what you need, at the frequency you need it. Daily prices rarely need a run every five minutes.
If a site blocks you, treat that as an answer. Do not try to get around it. Contact the owner or look for a licensed data source instead.
Is web scraping legal?
In brief: collecting publicly visible data is often lawful, but it is not free of rules. A site's terms of service can bind you as a contract, especially if you log in. Copyright can protect the content itself. Privacy law applies to personal data wherever you found it, including India's Digital Personal Data Protection Act, 2023.
We cover the court cases and the Indian position in detail in is web scraping legal?. This is general information, not legal advice. For any project that touches personal data, logged-in areas or large volumes of copyrighted content, speak to a lawyer first.
What do businesses use web scraping for?
The common, legitimate uses are mostly about public, factual data:
- Price and stock monitoring of products sold online, as in our guide to building an e-commerce price monitor.
- Market research, such as tracking new listings, product ranges or job postings in a sector.
- Lead lists from public directories, within privacy law and the directory's terms.
- Aggregating public records, such as tenders, regulatory notices or property listings.
- Feeding internal tools and AI systems with up-to-date public information, where the licence allows it.
In each case the value is not the scraper. It is clean, reliable data arriving on time, with someone alerted when it stops.
Frequently asked questions
What is the difference between web scraping and web crawling?
Crawling is discovering pages by following links, which is what search engines do. Scraping is extracting specific data from pages. Most scrapers crawl a little, to find the pages they need, and then scrape each one.
Why does my scraper return empty results?
Usually the page is rendered with JavaScript, so the data is not in the HTML you fetched. Check the page source and the Network tab. The other common cause is that the site changed its layout and your selectors no longer match.
Can I scrape a site that requires a login?
Logging in usually means you have accepted the site's terms, and those often forbid automated collection. It also raises the legal risk considerably. Use an official API or ask the site owner for access instead.
How often should I run a scraper?
As often as the data really changes and you really need it. Prices might be daily, public tenders hourly, a company directory monthly. Fewer runs mean less load on the site and fewer chances to break.
Where to start
Start by writing down exactly which fields you need, from which sites, and how often. Then check for an API, read robots.txt and the terms, and build the smallest scraper that works, with monitoring from day one.
If you would rather have a reliable, policy-respecting data pipeline built and maintained for you, see our data scraping service.

