Introduction
The internet is full of data, from product prices to research papers, sports statistics to stock market trends. But how do we actually collect this data in a structured way? That’s where web scraping comes in. This is a subject I’ve recently been immersing myself in after completing my MSc in Data Science & Analytics at Royal Holloway, University of London. I’ve found that one of the best ways to truly master a subject is not only to practice it, but also to teach and write about it. That’s exactly what I plan to do with this blog series. I’d describe myself as being at an intermediate level with web scraping right now. So, this first post will serve as the starting point of a series where we’ll explore the fundamentals step by step. Today, we’ll cover:
- What web scraping actually is
- Important caveats to keep in mind
- Different types of scraping
- And finally, we’ll build a tiny scraper using Python’s
requestsandBeautifulSoup
The Bare Minimum: What You Need to Know
Before diving into code, let’s start with the basics.
What is Web Scraping?
Web scraping is the process of programmatically extracting information from websites. Instead of copying and pasting manually, you write a script that does the heavy lifting for you turning unstructured web pages into structured data you can actually work with.
Legal & Ethical Considerations
Scraping can be incredibly useful, but it also comes with responsibilities. Before pointing your scraper at a website, here are a few things you should always keep in mind when web scraping
- Respect Terms of Service (ToS)
Scraping can be incredibly useful, but it also comes with responsibilities. Before pointing your scraper at a website, here are a few things you should always keep in mind when web scraping - Always check robots.txt
Every well-behaved scraper should respect a website’s robots.txt file. This is a small text file located at the root of a website (e.g. https://example.website.com/robots.txt, https://www.amazon.com/robots.txt). It tells web crawlers (like Googlebot, or your scraper) which pages are allowed to be crawled and which are off limits.
Example:User-agent: *
This means all bots should not scrape pages inside /private/. Should also note that while robots.txt isn’t a legal barrier (it’s not enforced by code as of 2023), ignoring it is considered bad practice and could get your scraper blocked.
Disallow: /private/
- Respect Terms of Service (TOS)
Most websites have Terms of Service (ToS) that explicitly state what you can and cannot do with their content. Some sites allow scraping for personal or educational use. Others forbid automated data collection completly. So always slim the ToS or have an AI read the ToS for you and see if it mention allowing scrapers. Especially if you plan to use the data commercially which is something I have expereince in and have defintely not broken! - Don't Overlod Servers
Imagine hundreds of scrapers hammering a website at once. The horror for anyone hosting a personal website on a 4GB Raspberry Pi! It could crash the server and disrupt the site for real users. Be a polite scraper by adding in delays (time.sleep()), use rate limiting, and avoid sending too many requests per second. I like to think scraping as going to the gym, yes, I said gym. On the dumbbell rack, oly take the dumbbel weight you intend to use at the time for your sets. Don't be an unpolite person and grab half the dumbbell racks for yourself.
- Avoid Restricted or Paywalled Data
Just because you can scrape something, doesn’t mean you should! If a site requires a login, bypassing it with scraping may break the site’s ToS. If content is behind a pywall, scraping it for free access us definetly unethical (potentially illegal). With the likes of sensitive data like personal information of client should never be scraped without permission. Especially for commerical!
Types of Web Scraping
Not all websites are created equal — and the way you scrape depends on how the site delivers its content. Broadly, there are three main categories of scraping:
- Static HTML Pages
These are the simplest website, not only to scrape. As statuc HTML pages don't have a backend server, or are connected to a remote database or any fancy css and javascript. Think of this like reading a printed newspaper. The content is already there, no extra effort is needed with flashing lights of adverts side to side or requiring you click the next page button or forced watch of an advert to access the rest of the article. The HTML you get from a requests.get() call is exactly what you see in your browser. Example is a plain blog page or an online article where the text and images are all right there in the pages source. Libraries you ise is the requests and beutifulSoup. - Dynamic Pages (JavaScript-Rendered)
Some sites don’t show all their content immediately. Instead, they load data with JavaScript after the page initially loads and/or from user input like a search bar. If you just fetch the HTML, you’ll often get a nearly empty page. Examples of this include social media or ecommerce sites where you if you fetch the empty html the website content of social media post would be empty. Think of it like watching a TV show live, the content appears only after the “script” (JavaScript) runs. Many modern and high traffic website operate like this or in a hbrid approach of the first two listed. - API Scraping
Some websites expose an API (Application Programming Interface) for structured access to their data. Instead of parsing HTML, you can send requests to endpoints like https://api.example.com/product. APIs are usually faster, cleaner, and more reliable than HTML scraping. Example: Twitter (X) API (before it went paid), weather APIs or reddit api. Think of this as ordering fast food, you order from their menu (documentation) and use that to make a request. You will hopefully get what you asked for (except if it is Mcdonalds) and they package it up neatly for you. - Hybrid Cases
Sometimes, sites mix methods. For example, an e-commerce site might serve basic product info in the HTML, but load reviews dynamically with JavaScript, and also provide an API for search. Scraping then becomes a puzzle of figuring out which method is most efficient (and ethical).
Quick recap is that static pages is the easist to scrape as HTML is ready. Dynamic pages need a browser like tools. APIs provide the cleanest format but only if their avialable. Hybrid is a mix of the above and will require some setective work.
Writing a Very Basic Scraper
Now that we’ve covered the types of scraping, let’s get our hands dirty with some code. We’ll start simple: scraping text from a static HTML page using just two Python libraries — requests and BeautifulSoup.
pip install requests beautifulsoup4
Scraping Quotes
There is a great practice website called http://quotes.toscrape.com. It's specifically built for learning web scraping, no legal or ethical worries here.
Lets fetch the homepage and grab all quotes
import requests
from bs4 import BeautifulSoup
# Step 1: Send a GET request to the site
url = "http://quotes.toscrape.com"
response = requests.get(url)
# Step 2: Parse the HTML with BeautifulSoup
soup = BeautifulSoup(response.text, "html.parser")
# Step 3: Find all quotes (they’re inside tags)
quotes = soup.find_all("span", class_="text")
# Step 4: Print them out
print("Quotes from the homepage:")
for q in quotes:
print("-", q.get_text())
How Do We Know Which Tags to Query?
This is the heart of web scraping. You need to inspect the website’s HTML (right-click → “Inspect” in Chrome/Firefox) to see how the page is structured.
For example, if we inspect the quotes site, we’ll find each quote is wrapped like this:
<span class="text">“The world as we have created it…”</span>
<span>by <small class="author">Albert Einstein</small></span>
- The quote text is inside a
<span>tag withclass="text" - The author name is inside a
<small>tag withclass="author"
That why we can query them like this:
quotes = soup.find_all("span", class_="text")
authors = soup.find_all("small", class_="author")
for q, a in zip(quotes, authors):
print(f"{q.get_text()} — {a.get_text()}")BeautifulSoup Querying Basics
There are two main ways to find elements:
- By tag name and attribute
soup.find("span", class_="text") # first <span class="text">
soup.find_all("small", class_="author") # all <small class="author">
soup.find("div", id="main") # <div id="main"> - By CSS selector (familair if you know CSS)
soup.select("span.text") # all <span class="text">
soup.select("div.quote > span") # <span> directly inside <div class="quote">
soup.select("small.author") # all <small class="author">
The CSS selector method (.select()) is often cleaner when the page has complex nested structures.
More Resources
- BeautifulSoup official docs on searching the tree
- A good cheatsheet on W3School for CSS selectors
Recap
In this post, we explored the fundamentals of web scraping from the ground up. We began by defining what scraping actually is and highlighted the important legal and ethical considerations such as respecting robots.txt, checking Terms of Service, and making sure we don’t overload servers. From there, we broke down the main types of scraping — static HTML pages, dynamic JavaScript-rendered sites, APIs, and hybrid cases that mix the three. With that foundation in place, we built a very basic scraper in Python using the requests and BeautifulSoup libraries. Along the way, we learned how to query HTML elements by their tags, attributes, and CSS selectors, which is the core skill for extracting the exact pieces of information you want.
By the end of this exercise, you now have everything you need to pull structured data from simple static web pages. It’s a small but powerful step into the world of web scraping.