Hint
🧠 Not sure how to start this project?
Here’s how you can break it down step by step:
Fetch the web page
Use therequests
library to download the HTML content:
import requests
from bs4 import BeautifulSoup
URL = "http://books.toscrape.com/"
response = requests.get(URL)
Parse the HTML with BeautifulSoup
So you can easily extract the book titles and prices:
soup = BeautifulSoup(response.content, "html.parser")
Find all the book entries on the page
Each book is inside an<article>
tag with class"product_pod"
:
books = soup.find_all("article", class_="product_pod")
Extract the title and price
From eacharticle
element:
for article in books:
title = article.h3.a["title"]
price = article.find("p", class_="price_color").text.strip()
print(title, price)
Save to an SQLite database
Usesqlite3
to create abooks
table (if it doesn’t exist yet) and insert each record:
import sqlite3
conn = sqlite3.connect("books.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY, title TEXT, price TEXT)")
Insert all records efficiently
Withexecutemany()
:
c.executemany("INSERT INTO books (title, price) VALUES (?, ?)", list_of_books)
conn.commit()
conn.close()
✅ That’s all you need. Click Show Solution in the project page to see how it all ties together in a clean end-to-end script.