Tu fais comme ça
import csv
from typing import Dict, List
import requests
from bs4 import BeautifulSoup
def get_all_products_from_url(url: str, session: requests.Session) -> List[Dict]:
headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:93.0) Gecko/20100101 Firefox/93.0"}
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
product_list = soup.find("ul", {"class": "srp-results"})
all_products = product_list.find_all("li", {"class": "s-item"})
products = []
for product in all_products:
product_dict = {}
product_dict["url"] = product.find("a", {"class": "s-item__link"}).attrs["href"]
product_dict["name"] = product.find("h3", {"class": "s-item__title"}).text
product_dict["price"] = product.find("span", {"class": "s-item__price"}).text
products.append(product_dict)
return products
def write_products_to_csvfile(products: List[Dict], filename="ebay-products.csv") -> None:
with open(filename, mode="w", encoding="utf-8") as csvfile:
fieldnames = ["url", "name", "price"]
csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
csvwriter.writeheader()
csvwriter.writerows(products)
if __name__ == "__main__":
url = "https://www.ebay.fr/sch/i.html?_from=R40&_trksid=p2380057.m570.l1313&_nkw=thinkpad&_sacat=0"
session = requests.Session()
# On met des cookies dans la session
session.get("https://www.ebay.fr/")
products = get_all_products_from_url(url, session=session)
write_products_to_csvfile(products)