Web Development

Building a Web Scraper in Python and PHP

There are over a billion websites and almost none of their data comes in a format made for you. A scraper is how you get it anyway: fetch the page, parse the HTML, keep what you need.

The shape of every scraper

Two halves — retrieval and parsing. Retrieval is an HTTP client (Python's requests, PHP's cURL); parsing is a DOM library that turns markup into something addressable (BeautifulSoup, lxml, DOMDocument). The structure never changes:

import requests
from bs4 import BeautifulSoup

html = requests.get("https://example.com/articles",
                    headers={"User-Agent": "scraper/1.0"}).text
doc = BeautifulSoup(html, "html.parser")
for a in doc.select("article h2 a"):
    print(a.text, a["href"])

The CSS selector is where the work lives. Everything before it is boilerplate.

Where scrapers actually break

Not the parsing — the target. Pages that render with JavaScript need a headless browser, not requests. Rate limits and bot detection exist precisely because scrapers hammer servers; a scraper without time.sleep and a real User-Agent is how IP bans happen. And sites change: the selector that worked in March silently returns nothing in June.

Publicly visible data, respectfully fetched, is broadly defensible — hiQ v. LinkedIn settled that scraping public pages isn't a CFAA violation. The line is drawn by the ToS, robots.txt convention, and authenticated areas. Scraping behind a login, or hammering a server into degraded service, is where "public data" stops protecting you.

$ tail -f /forums

Discussion

Comments live on the community forums — sign in there to reply.