import time
import requests
import json
import csv
import re
from bs4 import BeautifulSoup
import pandas as pd
def my_func(site, cat):
    urlr = requests.get(site + '0' + cat).text
    soup1 = BeautifulSoup(urlr, 'lxml')
    ran1 = soup1.find('p', {'id':'pageFound'}).text.replace(' productos', '')
    ran = int(ran1.split('de')[1])
    for i in range(0, ran, 24):
        url = requests.get(site + str(i) + cat).text
        soup = BeautifulSoup(url, 'lxml')
        divs = soup.find_all('article', class_='product-card-item')
        for div in divs:
            try:
                link = requests.get('https://www.carrefour.es' + div.find('a')['href']).text
                suop1 = BeautifulSoup(link, 'lxml')
                print('https://www.carrefour.es' + div.find('a')['href'])

                name = suop1.find('h1', class_='title-03').text
                cat1 = suop1.find('div', {'id':'breadcrumbs-top'}).find_all('li')[2].text
                cat2 = suop1.find('div', {'id':'breadcrumbs-top'}).find_all('li')[3].text
                try:
                    cat3 = suop1.find('div', {'id':'breadcrumbs-top'}).find_all('li')[4].text
                except Exception:
                    cat3 = ''
                pricekg = suop1.find('p', class_='price-kg').text.replace(',', '.').replace('€', '')
                price = suop1.find('p', class_='price js-price').text.replace(',', '.').replace('€', '')
                data = 'https://www.carrefour.es' + div.find('a')['href']

                with open('www.carrefour.es.csv', 'a') as file:
                    a_pen = csv.writer(file, delimiter=';', quotechar='"')
                    a_pen.writerow((cat1, cat2, cat3, name, price, pricekg, data))
            except Exception:
                pass


my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Zwhajzd?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%29OR%29&prtId=cat20002')
my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Zv6agxv?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%2ConSaleSalePoints%3A004320%29OR%29&prtId=cat20001')
my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Zc7a800?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%2ConSaleSalePoints%3A004320%29OR%29&prtId=cat20003')
my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Z9h2xf1?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%2ConSaleSalePoints%3A004320%29OR%29&prtId=cat20006')
my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Zwga3su?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%2ConSaleSalePoints%3A004320%29OR%29&prtId=cat20005')
my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Z1g5bcwn?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%2ConSaleSalePoints%3A004320%29OR%29&prtId=cat20004')
my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Zg9acu1?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%2ConSaleSalePoints%3A004320%29OR%29&prtId=cat20008')
my_func('https://www.carrefour.es/supermercado/ofertas/N-177ap79Z17wty43?No=', '&Nr%3DAND%28product.shopCodes%3A004320%2Cproduct.salepointWithActivePrice_004320%3A1%2COR%28product.siteId%3AbasicSite%29%2ConSaleSalePoints%3A004320%29OR%29&prtId=cat20007')