Python Web Scraping Guide
Web scraping is the process of extracting data from websites. Python is the go-to language for this due to its powerful libraries like requests and BeautifulSoup.
Prerequisites
The Workflow
- Inspect: Use your browser's Developer Tools to understand the structure of the website.
- Request: Fetch the HTML content using
requests. - Parse: Parse the HTML using
BeautifulSoup. - Extract: Find the specific data you need.
- Store: Save the data (CSV, JSON, Database).
Example: Scraping Quotes
We will scrape quotes from http://quotes.toscrape.com.
import requests
from bs4 import BeautifulSoup
import csv
# 1. Request the page
url = "http://quotes.toscrape.com"
response = requests.get(url)
# Check if request was successful
if response.status_code == 200:
# 2. Parse the HTML
soup = BeautifulSoup(response.text, "html.parser")
quotes_data = []
# 3. Extract Data
# Find all elements with class "quote"
quotes = soup.find_all("div", class_="quote")
for quote in quotes:
text = quote.find("span", class_="text").get_text()
author = quote.find("small", class_="author").get_text()
tags = [tag.get_text() for tag in quote.find_all("a", class_="tag")]
quotes_data.append({
"text": text,
"author": author,
"tags": ", ".join(tags)
})
# 4. Store Data
with open("quotes.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["text", "author", "tags"])
writer.writeheader()
writer.writerows(quotes_data)
print("Scraping complete. Saved to quotes.csv")
else:
print("Failed to retrieve the page")
Best Practices & Ethics
- Check
robots.txt: Always checkwebsite.com/robots.txtto see if scraping is allowed. - Rate Limiting: Do not hammer the server with requests. Use
time.sleep()between requests. - User-Agent: Some sites block default python user agents. Set a custom one in headers.
headers = {'User-Agent': 'Mozilla/5.0 (MyScraperBot 1.0)'}
requests.get(url, headers=headers)