-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebScrap.py
More file actions
29 lines (22 loc) · 939 Bytes
/
WebScrap.py
File metadata and controls
29 lines (22 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
from bs4 import BeautifulSoup
import csv
# Send an HTTP request to the website
url = "http://books.toscrape.com/"
response = requests.get(url)
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find all product items on the page
product_items = soup.find_all('article', class_='product_pod')
# Create a CSV file to store the data
with open('products.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Title", "Price", "Rating"]) # header row
# Extract product information from each item
for item in product_items:
title = item.find('h3').text
price = item.find('p', class_='price_color').text
rating = item.find('p', class_='star-rating').get('class')[1]
# Write the data to the CSV file
writer.writerow([title, price, rating])
print("Data extracted and saved to products.csv")