add tests, first try to scrape all title, prices

This commit is contained in:
yann 2024-11-12 18:20:27 +01:00
parent c5b1114e70
commit fd53a2d704

32
tests/test.py Normal file
View File

@ -0,0 +1,32 @@
# test de scrapping de BookToScrap
import requests
from bs4 import BeautifulSoup
import csv
produits=[]
livre=[]
for i in range(1,51):
url="https://books.toscrape.com/catalogue/page-"+str(i)+".html"
print(url)
r = requests.get(url, )
page = r.content
soup = BeautifulSoup(page, "html.parser")
for i in soup.find_all("article"):
nom=i.h3.a.get('title')
prix=i.find('p', class_="price_color").string
livre.append((nom, prix))
# création du fichier CSV et liste d'entête
entete=['titre', 'prix']
with open('data.csv', 'w') as fichier:
writer = csv.writer(fichier, delimiter=',')
writer.writerow(entete)
for i in livre:
writer.writerow(i)
print(livre)
print(len(livre))