mardi 19 juin 2018

Scraping python

Can someone tell me why this code is returning the first page for multiple times, please?

import csv

import requests

import datetime

from bs4 import BeautifulSoup   

 

sf = 3


name = "aragaz" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")+".csv"

with open(name, "w+") as f:

 

    writer = csv.writer(f)

    writer.writerow(["Date","Inf","Actual"])

    y = 1

   

    while y <= sf:

 

        r = requests.get("https://altex.ro/apa-espresso-cafea/cafetiere/filtru/p/".format(y))

 

        soup = BeautifulSoup(r.text, "html.parser")

        x = soup.find_all("li", {'class': 'Products-item'})

        y += 1

        for Products in x:

            Date = Products.find('h2', {'class': "Product-nameHeading"}).text.strip()

            Inf = Products.find('div', {'class': "Price"}).text.strip()

           

           

            writer.writerow([Date, Inf, datetime.datetime.now()])

 




Aucun commentaire:

Enregistrer un commentaire