-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapbooks.py
More file actions
28 lines (23 loc) · 806 Bytes
/
scrapbooks.py
File metadata and controls
28 lines (23 loc) · 806 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
import pandas as pd
import requests
from bs4 import BeautifulSoup
import pandas as pd
books =[]
for i in range(1,51):
url = f"https://books.toscrape.com/catalogue/page-{i}.html"
response = requests.get(url)
response = response.content
soup = BeautifulSoup(response, 'html.parser')
ol = soup.find('ol')
articles = ol.find_all('article', class_='product_pod')
for article in articles:
image = article.find('img')
title = image.attrs['alt']
star = article.find('p')
star = star['class'][1]
price = article.find('p', class_='price_color').text
price = price[1:]
price = float(price)
books.append([title, price, star])
df = pd.DataFrame(books, columns=['Title', 'Price', 'Ratings'])
df.to_csv('books.csv')