Today I Learned How to Scrape Book Data from a Website Using Python!

Hey everyone! 👋
Today I dove into the world of web scraping for the first time — and I want to share exactly what I learned. It’s super exciting to write a Python script that grabs real data from a website and saves it to a file. No more copy-pasting manually!
What I Scraped
I chose this cool practice site called BooksToScrape.com. It’s made just for scraping, so it’s perfect for beginners like me.
From the first page, I was able to get:
The title of each book
The price
Whether it’s in stock
The book’s rating (like 1 to 5 stars)
The Code I Wrote
Here’s the Python script that made it all happen:
import requests
from bs4 import BeautifulSoup
import csv
url = "https://books.toscrape.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
books = soup.find_all("article", class_="product_pod")
with open("scrapped.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["Title", "Price", "Availability", "Rating"])
for book in books:
title = book.h3.a["title"]
price = book.find("p", class_="price_color").get_text()
availability = book.find("p", class_="instock availability").get_text(strip=True)
rating_map = {
"One": 1,
"Two": 2,
"Three": 3,
"Four": 4,
"Five": 5
}
rating_word = book.find("p", class_="star-rating")["class"][1]
rating = rating_map.get(rating_word, 0)
writer.writerow([title, price, availability, rating])
print("DONE!")
How It Works
I used
requests
to fetch the webpage.BeautifulSoup
helped me parse the HTML and find the book info.Each book is inside an
<article>
tag with the class"product_pod"
.For each book, I grabbed the title, price, availability, and rating.
The rating was a bit tricky because it’s hidden inside a class name like
"Three"
— so I created a dictionary to convert that to a number.Finally, I saved everything to a CSV file.
What I Got
When I opened the CSV, I saw neat rows like:
Title | Price | Availability | Rating |
A Light in the Attic | £51.77 | In stock | 3 |
Tipping the Velvet | £53.74 | In stock | 1 |
(And lots more!)
What’s Next?
Right now, my script only scrapes the first page. But this site has multiple pages — so my next step is to figure out how to:
Click through to the next page automatically,
Scrape every page,
And save all that data in one big CSV file.
I can’t wait to share that journey with you all next time!
Final Thoughts
Web scraping feels powerful and fun! I’m excited to keep learning and improve my skills. If you’re curious about web scraping, I highly recommend trying out BooksToScrape.com — it’s a great way to start.
Thanks for reading my first web scraping post!
Subscribe to my newsletter
Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
