devto 2026-07-27 원문 보기 ↗
In this guide, you will learn how to write a Python script that visits a website, grabs product names and prices, and saves everything into a spreadsheet file. No experience needed — we explain every step.
You can find the complete source code for this project in this GitHub repository.
for loops)We need to install 3 extra tools for Python. Open your terminal and copy-paste this line:
pip install requests beautifulsoup4 pandas
Then press Enter and wait for it to finish.
📦
What are these tools?
.csv file)First, we tell Python which website to visit and download its content.
import requests
from bs4 import BeautifulSoup
import pandas as pd
# The website you want to scrape
URL = "https://toscrape.com"
# This tells the website our script is a normal browser (not a robot)
headers = {
"User-Agent": "Mozilla/5.0"
}
# Visit the website and download the page
response = requests.get(URL, headers=headers)
# If something goes wrong, Python will tell us
response.raise_for_status()
print("Page downloaded successfully!")
💡
The User-Agent line is important. Without it, some websites will refuse our script because they think it's a robot.
Every website is built with HTML — a language that describes what goes where on a page. We use BeautifulSoup to read that HTML and find the products.
# Read the HTML of the page
soup = BeautifulSoup(response.text, "html.parser")
# Find all product cards on the page
# On toscrape.com, each product is inside an <article> tag with class "product_pod"
products = soup.find_all("article", class_="product_pod")
print(f"We found {len(products)} products!")
🔍
How do you know which class name to use?
class="..." name around that productNow we go through each product one by one and grab its name and price.
# This list will store all our products
data = []
for product in products:
# Find the name of the product
name = product.h3.a if product.h3 else None
# Find the price of the product
price = product.find("p", class_="price_color")
# Save the name and price (or "N/A" if not found)
# We use name["title"] because the full product name is stored in the title attribute
data.append({
"name": name["title"] if name and name.has_attr("title") else "N/A",
"price": price.get_text(strip=True) if price else "N/A"
})
# Show the first 3 results to check everything looks good
print(data[:3])
Finally, we take all the data we collected and save it as a .csv file (which you can open in Excel or Google Sheets).
# Turn the list into a table
df = pd.DataFrame(data)
# Save the table as a CSV file
df.to_csv("products.csv", index=False, encoding="utf-8")
print(f"✅ Done! {len(df)} products saved to products.csv")
| Problem | What it means | How to fix it |
|---|---|---|
403 Forbidden |
The website blocked your script | Make sure you added the User-Agent header |
AttributeError: NoneType |
The name or price was not found on the page | Check that the class names in your code match the website's HTML |
| Empty results (0 products found) | The website loads products with JavaScript, not HTML | This guide won't work — you'll need a more advanced tool like Selenium |
ConnectionError |
No internet or the website is down | Check your connection and try again |