Web Scraping With Python: A Beginner's Guide
Learn the basics of web scraping with Python using requests and BeautifulSoup, and the rules that keep you polite and safe: robots.txt, terms of service, rate limits and personal data.
Short answer: web scraping with Python means downloading a web page with the requests library and pulling out the parts you need with a parser such as BeautifulSoup. Before you write any code, check the site's robots.txt and terms of service, go slowly, and avoid collecting personal data.
What is web scraping, and when should you use it?
Web scraping is using a program to read a web page and extract data from its HTML, for example a list of article titles or product names. It is useful for research, price comparison for your own use and building small datasets for practice.
Always ask first: does the site offer an API? An API returns clean data in a format built for programs, and it is the intended way to get it. Scraping is the fallback when there is no API. If you are new to APIs, read What Is an API and How Does It Work?.
Is web scraping allowed?
Sometimes yes, sometimes no, and the rules differ by site and by place. This is not legal advice, so check the rules that apply to you. Before scraping any site, look at these four things:
robots.txt: a file at the root of a site (for example
https://example.com/robots.txt) that says which paths automated programs should not visit, and sometimes how fast to go.Terms of service: many sites forbid automated access or reuse of their content. If the terms say no, do not scrape.
Rate limits: send requests slowly. Hammering a small server can slow it down or break it for others.
Personal data: names, emails, photos and profiles belong to real people. Privacy rules may apply even when the data is public, so do not collect it without a clear, lawful reason.
How do you check robots.txt in Python?
The standard library has a parser for it. This example reads rules from text, so it runs without a network:
from urllib.robotparser import RobotFileParser
rules = [
"User-agent: *",
"Disallow: /private/",
"Crawl-delay: 5",
]
rp = RobotFileParser()
rp.parse(rules)
print(rp.can_fetch("MyBot", "https://example.com/books"))
print(rp.can_fetch("MyBot", "https://example.com/private/data"))
print(rp.crawl_delay("MyBot"))
True
False
5
For a real site you would call rp.set_url("https://example.com/robots.txt") and then rp.read(). A Crawl-delay of 5 asks bots to wait about 5 seconds between requests.
How do you scrape a simple page?
Install the two libraries with pip install requests beautifulsoup4. Then fetch the page, parse it and select what you want. The address below is a placeholder for a site that you have checked and are allowed to scrape:
import time
import requests
from bs4 import BeautifulSoup
url = "https://example.com/articles"
headers = {"User-Agent": "learning-scraper (contact: [email protected])"}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # stop on 404, 500 and similar errors
soup = BeautifulSoup(response.text, "html.parser")
for link in soup.select("h2 a"):
print(link.get_text(strip=True), link["href"])
time.sleep(5) # wait before the next request
Here is what each part does:
requests.getdownloads the page. Atimeoutstops it hanging forever.The
User-Agentsays who you are, which is polite and lets site owners contact you.soup.select("h2 a")uses a CSS selector to find every link inside anh2. Use your browser's "inspect" tool to find the right selector for your page.time.sleepkeeps your request rate low.
To save results, write them to a file; see Python files, JSON and CSV. For more on the requests library, see Python requests and web APIs.
What if the data is not in the HTML?
Some pages load their data with JavaScript after the page opens, so requests only sees an empty shell. Open your browser's network tab: often the page is calling a JSON endpoint, and if the site's terms allow it, that is easier than scraping HTML. Heavy tools that run a full browser exist, but they are slower and are a step you rarely need as a beginner.
Common mistakes
Skipping
robots.txtand the terms of service.Sending requests in a fast loop with no delay.
Scraping personal data "because it is public".
Writing selectors that break when the page layout changes, with no error handling.
Ignoring status codes: always check the response before parsing.
Key Takeaway
Use an API if one exists. If you scrape, do it politely: read robots.txt and the terms, send requests slowly, identify yourself and stay away from personal data. The code is short; the responsibility is the real lesson.
Ready to start? It teaches the Python basics, files and requests that scraping is built on. Begin with the free course Python for AI: From Zero to Your First AI App.
Related articles
More in Python
Python Virtual Environments (venv) Explained
A Python venv keeps each project's packages separate so projects cannot break each other. Learn the exact commands for macOS, Linux and Windows, plus requirements.txt.
5 min readNov 29, 2026What Is scikit-learn? The First Machine Learning Library to Learn
scikit-learn is the standard Python library for classic machine learning. See what it does, how to install it, and a complete first model in a few lines.
4 min readNov 27, 2026Clean CSV Data With Pandas: 10 Useful Commands
Ten pandas commands that fix most messy CSV files: spaces, capitals, duplicates, wrong types and missing values, shown in one example you can run.
4 min readNov 21, 2026TensorFlow vs PyTorch: Which Should Beginners Learn?
TensorFlow and PyTorch teach the same core ideas. Compare them for beginners, see what they automate with a small runnable Python example, and learn how to choose without wasting weeks.
4 min readNov 19, 2026Jupyter vs VS Code vs Google Colab
Where should you write your first Python code? Compare Jupyter Notebook, VS Code and Google Colab, with a simple rule for choosing.
4 min readNov 15, 2026Your First Python Project: Build a Calculator
Build a command-line calculator in about 30 lines of Python, learning functions, loops, conditions and error handling along the way.
3 min readNov 12, 2026
Comments
Sign in to join the discussion.

