Skip to content

Web Scraping Introduction

A web crawler is a program that simulates browser behavior to automatically fetch data from the internet. Python has become the go-to language for web scraping thanks to its rich ecosystem (requests, BeautifulSoup, Scrapy, etc.).

What Is a Web Crawler

When a browser visits a web page, it sends an HTTP request to a server and renders the returned HTML/JSON response. A crawler reproduces this process programmatically:

  1. Send an HTTP request (simulating a browser)
  2. Receive the response (HTML, JSON, images, etc.)
  3. Parse the content and extract target data
  4. Store the data (files, databases, etc.)

Types of Web Crawlers

TypeCharacteristicsUse Cases
General crawlerFetches the entire content of each pageSearch engine indexing (Googlebot, etc.)
Focused crawlerExtracts only specific data from a pagePrice monitoring, news collection
Incremental crawlerOnly fetches new or updated contentNews feeds, e-commerce updates
Distributed crawlerMultiple machines work in parallel on massive datasetsLarge-scale data collection (Scrapy-Redis)

Focused crawlers are usually built on top of general crawlers: fetch the complete page first, then extract the required fields.

Legality of Web Scraping

When using a crawler, keep the following principles in mind:

  • Respect robots.txt: Check the robots.txt file at the root of the target site to see which paths are disallowed.
  • Do not disrupt normal service: Control your request rate to avoid putting excessive load on the server (DDoS-like behavior is illegal).
  • Comply with data regulations: Do not scrape or distribute infringing content (user privacy, copyrighted material, etc.).
  • Obtain authorization for commercial use: If you intend to use scraped data commercially, get written permission from the site owner first.
If a crawler causes a server to become unable to provide normal service, or if it collects data involving personal privacy or trade secrets, it may violate laws such as the Cybersecurity Law and the Data Security Law.

Anti-Scraping and Counter-Measures

Websites typically deploy anti-scraping mechanisms, and crawlers need corresponding countermeasures:

Anti-scraping TechniqueDescriptionCountermeasure
User-Agent detectionIdentifies non-browser request headersSpoof the User-Agent
IP rate limiting / blockingLimits requests per IPRandom delays + proxy IP pool
Cookie / SessionRequires a logged-in stateSimulate login, maintain session
CAPTCHAHuman verification challengeCAPTCHA-solving service / AI recognition
JS renderingData generated dynamically by JavaScriptSelenium / Playwright
Data encryptionAPI parameters are signedReverse-engineer the encryption algorithm

Typical Workflow

import requests
from bs4 import BeautifulSoup

url = "https://books.toscrape.com/"
headers = {"User-Agent": "Mozilla/5.0 (compatible; MyBot/1.0)"}

resp = requests.get(url, headers=headers, timeout=10)
resp.raise_for_status()

soup = BeautifulSoup(resp.text, "html.parser")

for article in soup.select("article.product_pod"):
    title = article.h3.a["title"]
    price = article.select_one(".price_color").text
    print(f"{title}: {price}")

This example demonstrates the three fundamental steps of a crawler: request → parse → extract. The following chapters will cover each step in depth.

Last updated on