33 lines
748 B
Python
33 lines
748 B
Python
# 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))
|
|
|