Skip to main content

Command Palette

Search for a command to run...

Your First Python Web Scraper: Zero Setup, Just Click and Run

Published
โ€ข5 min readโ€ขView as Markdown

Want to learn web scraping but don't want to mess with installing Python, libraries, and dependencies? You're in the right place.

Today, you'll build your first web scraper using Google Colab - no installation required. Just click a link and start scraping.

What You'll Build

We'll scrape a simple online bookstore and extract:

  • Book titles

  • Prices

  • Ratings

  • Categories

  • Stock information

By the end, you'll have working code that turns messy HTML into clean, usable data.

Before We Start

What you need:

  • A Google account (that's it!)

  • 10 minutes

What you DON'T need:

  • Python installed

  • Command line knowledge

  • Any setup whatsoever

Step 1: Open Google Colab

๐Ÿ‘‰ Click here to open the tutorial in Google Colab (you'll create this link after making the notebook)

Or manually:

  1. Go to colab.research.google.com

  2. File โ†’ New Notebook

Step 2: Install Libraries

Copy this into the first cell and run it (Shift+Enter):

# Install required libraries (takes 10 seconds)
!pip install beautifulsoup4 requests
print("โœ… Libraries installed!")

That's it. Setup done. Now let's scrape.

Step 3: Your First Scrape

I've created a simple bookstore page for us to practice on. It's hosted on GitHub Pages, so it's always available.

See the page here: https://umanggulati.github.io/python-demo/products.html

Now let's scrape it. Copy this into a new cell:

import requests
from bs4 import BeautifulSoup

# Fetch the page
url = 'https://umanggulati.github.io/python-demo/products.html'
response = requests.get(url)

# Parse the HTML
soup = BeautifulSoup(response.content, 'html.parser')

print("โœ… Page loaded successfully!")
print(f"๐Ÿ“„ Page title: {soup.title.string}")

Run it. If you see "Page loaded successfully!", you just scraped your first webpage!

Step 4: Extract Book Titles

Let's get all the book titles:

# Find all product titles
titles = soup.find_all('h2', class_='product-title')

print(f"๐Ÿ“š Found {len(titles)} books:\n")
for title in titles:
    print(f"  โ€ข {title.text}")

Output:

๐Ÿ“š Found 5 books:

  โ€ข Python Crash Course, 3rd Edition
  โ€ข Fluent Python, 2nd Edition
  โ€ข Automate the Boring Stuff with Python
  โ€ข Python for Data Analysis, 3rd Edition
  โ€ข Flask Web Development, 3rd Edition

See how easy that was? .find_all() grabs every matching element. .text extracts the text.

Step 5: Extract Prices

# Find all prices
prices = soup.find_all('div', class_='price')

print("๐Ÿ’ฐ Book prices:\n")
for price in prices:
    print(f"  {price.text}")

Output:

๐Ÿ’ฐ Book prices:

  $39.99
  $54.99
  $34.99
  $49.99
  $44.99

Step 6: Clean the Prices

Those strings aren't useful for math. Let's convert them to numbers:

# Extract and clean prices
prices = soup.find_all('div', class_='price')

print("๐Ÿ’ฐ Book prices (cleaned):\n")
for price in prices:
    # Remove $ and convert to float
    clean_price = float(price.text.strip('$'))
    print(f"  ${clean_price:.2f}")

# Calculate average
price_values = [float(p.text.strip('$')) for p in prices]
average = sum(price_values) / len(price_values)
print(f"\n๐Ÿ“Š Average price: ${average:.2f}")

Output:

๐Ÿ’ฐ Book prices (cleaned):

  $39.99
  $54.99
  $34.99
  $49.99
  $44.99

๐Ÿ“Š Average price: $44.99

Now you can do math with your scraped data!

Step 7: Extract Multiple Things at Once

Let's get title AND price together:

# Find all product cards
products = soup.find_all('div', class_='product-card')

print(f"๐Ÿ“ฆ Found {len(products)} products:\n")

for product in products:
    # Extract data from each product
    title = product.find('h2', class_='product-title').text
    price = product.find('div', class_='price').text

    print(f"  {title}")
    print(f"  โ””โ”€ {price}\n")

Output:

๐Ÿ“ฆ Found 5 products:

  Python Crash Course, 3rd Edition
  โ””โ”€ $39.99

  Fluent Python, 2nd Edition
  โ””โ”€ $54.99

  Automate the Boring Stuff with Python
  โ””โ”€ $34.99

  [...]

Step 8: Extract Categories

Each book has category tags. Let's get those:

products = soup.find_all('div', class_='product-card')

print("๐Ÿท๏ธ  Books by category:\n")

for product in products:
    title = product.find('h2', class_='product-title').text
    categories = product.find_all('span', class_='category')

    # Extract text from each category
    category_list = [cat.text for cat in categories]

    print(f"  {title}")
    print(f"  โ””โ”€ {', '.join(category_list)}\n")

Output:

๐Ÿท๏ธ  Books by category:

  Python Crash Course, 3rd Edition
  โ””โ”€ Beginner, Projects

  Fluent Python, 2nd Edition
  โ””โ”€ Advanced, Best Practices

  Automate the Boring Stuff with Python
  โ””โ”€ Beginner, Automation

  [...]

Step 9: Find Low Stock Items

Let's find books that are running low:

products = soup.find_all('div', class_='product-card')

print("โš ๏ธ  Low stock alert:\n")

for product in products:
    stock_element = product.find('p', class_='stock')

    # Check if it has the 'low' class
    if stock_element and 'low' in stock_element.get('class', []):
        title = product.find('h2', class_='product-title').text
        stock_text = stock_element.text
        print(f"  ๐Ÿšจ {title}")
        print(f"     {stock_text}\n")

Output:

โš ๏ธ  Low stock alert:

  ๐Ÿšจ Automate the Boring Stuff with Python
     In Stock: 3 units

Step 10: The Complete Scraper

Let's put it all together and save the data:

import requests
from bs4 import BeautifulSoup
import json

# Fetch and parse
url = 'https://umanggulati.github.io/python-demo/products.html'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# Find all products
products = soup.find_all('div', class_='product-card')

print(f"๐Ÿ“ฆ Scraping {len(products)} products...\n")

# Extract all data
products_data = []

for product in products:
    product_dict = {
        'id': product.get('data-product-id'),
        'title': product.find('h2', class_='product-title').text,
        'price': float(product.find('div', class_='price').text.strip('$')),
        'rating': product.find('div', class_='rating').text,
        'categories': [cat.text for cat in product.find_all('span', class_='category')],
        'stock': product.find('p', class_='stock').text,
        'description': product.find('p', class_='description').text.strip()
    }
    products_data.append(product_dict)

# Display as formatted JSON
print(json.dumps(products_data, indent=2))

print(f"\nโœ… Successfully scraped {len(products_data)} products!")

This outputs clean, structured data you can use anywhere!

Step 11: Save to CSV (Bonus)

Want to open this in Excel or Google Sheets?

import csv

# Save to CSV
with open('products.csv', 'w', newline='', encoding='utf-8') as f:
    writer = csv.DictWriter(f, fieldnames=['id', 'title', 'price', 'rating', 'categories', 'stock'])
    writer.writeheader()

    for product in products_data:
        # Convert categories list to string for CSV
        product_copy = product.copy()
        product_copy['categories'] = ', '.join(product['categories'])
        writer.writerow({k: product_copy[k] for k in ['id', 'title', 'price', 'rating', 'categories', 'stock']})

print("โœ… Saved to products.csv")

# Download the file (in Colab)
from google.colab import files
files.download('products.csv')

The CSV downloads automatically! Open it in Excel or Sheets.

What You Just Learned

In 10 minutes, you learned to:

  • Load and parse HTML

  • Find elements by class name

  • Extract text content

  • Navigate nested HTML

  • Clean and process data

  • Save to JSON and CSV

These skills work on any website. The same code. Just different class names.

Try It Now!

Don't just read. Run the code in Colab. Change things. Break it. Fix it. That's how you learn.

Open Google Colab and start scraping: colab.research.google.com


Quick Tips for Colab

Run a cell: Shift + Enter
Add new cell: Click "+ Code" or "+ Text"
Save your work: File โ†’ Save (auto-saves to Google Drive)
Download notebook: File โ†’ Download โ†’ Download .ipynb


Resources:

Happy scraping! ๐Ÿ

More from this blog

Figure it Out

15 posts