update
This commit is contained in:
449
scrapeje.py
449
scrapeje.py
@@ -9,13 +9,6 @@ import re
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
#Inizializzo liste
|
|
||||||
nome=[]
|
|
||||||
desc=[]
|
|
||||||
npezzi=[]
|
|
||||||
prezzo=[]
|
|
||||||
prezzoN=[]
|
|
||||||
|
|
||||||
|
|
||||||
restaurant_url=''
|
restaurant_url=''
|
||||||
def inputurl():
|
def inputurl():
|
||||||
@@ -25,154 +18,274 @@ def inputurl():
|
|||||||
print ("\nesempio: https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu")
|
print ("\nesempio: https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu")
|
||||||
restaurant_url = input('INSERISCI IL LINK DELLA PAGINA DEL RISTORANTE: ')
|
restaurant_url = input('INSERISCI IL LINK DELLA PAGINA DEL RISTORANTE: ')
|
||||||
#PER DEBUG
|
#PER DEBUG
|
||||||
#restaurant_url = 'https://www.justeat.it/restaurants-dolceirnerio/menu'
|
#restaurant_url = 'https://www.justeat.it/restaurants-pizzeriadelrondone-bologna/menu'
|
||||||
|
|
||||||
|
|
||||||
# if os.path.exists('rubrica.txt'):
|
nome=[]
|
||||||
# if os.path.exists('./DATI_RUBRICA'):
|
desc=[]
|
||||||
inputurl()
|
npezzi=[]
|
||||||
# wr = input('\nVuoi vedere la rubrica? [Y|N] ')
|
prezzo=[]
|
||||||
# if wr.upper() in ['YES', 'Y', 'SI', 'S']:
|
prezzoN=[]
|
||||||
# print( '\n', os.listdir('./DATI_RUBRICA'), '\n')
|
nrist=''
|
||||||
# scelta = input("Scegli un numero esistente o premi Enter per metterre un link: ")
|
restaurant_address=''
|
||||||
# files = os.listdir('./DATI_RUBRICA/')
|
tel=''
|
||||||
# for file in files:
|
result=''
|
||||||
# if scelta in file:
|
def stora_tutto():
|
||||||
# restaurant_url = (file)
|
global nome
|
||||||
# else:
|
global desc
|
||||||
# print ("numero inesistente")
|
global npezzi
|
||||||
# inputurl()
|
global prezzo
|
||||||
# else:
|
global prezzoN
|
||||||
# inputurl()
|
global soup
|
||||||
# else:
|
global nrist
|
||||||
# inputurl()
|
global restaurant_address
|
||||||
# else:
|
global tel
|
||||||
# inputurl()
|
global result
|
||||||
|
|
||||||
|
#Stora nome ristorante
|
||||||
|
nrist = soup.title.text[8:-32]
|
||||||
|
|
||||||
#SCRAPE
|
#Stora telefono del ristorante
|
||||||
driver = webdriver.Chrome()
|
pattern = re.compile(r'allergenPhoneNumber')
|
||||||
driver.get(restaurant_url)
|
script_tags = soup.find_all('script', string=pattern)
|
||||||
|
pattern = re.compile(r'"allergenPhoneNumber":"(\d+)"')
|
||||||
|
tel = re.search(pattern, script_tags[0].next)
|
||||||
|
if tel:
|
||||||
|
tel = tel.group(1)
|
||||||
|
|
||||||
wait = WebDriverWait(driver, 16)
|
#Stato ristorante
|
||||||
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "c-menuItems-price")))
|
restaurant_is_open = menu = soup.find(attrs={"data-js-test":"order-status-wrapper"}).text
|
||||||
|
restaurant_is_open = restaurant_is_open.replace('\n', ' ')
|
||||||
|
regex = r" {4,}"
|
||||||
|
result = re.split(regex, restaurant_is_open)
|
||||||
|
|
||||||
page = driver.page_source
|
#indirizzo ristorante
|
||||||
|
restaurant_address = soup.find(attrs={"data-js-test":"header-restaurantAddress"}).text
|
||||||
|
|
||||||
with open('JEmenu.html', 'w') as f:
|
#cicla le schede prodotto
|
||||||
f.write(page)
|
menu = soup.find(attrs={"data-test-id": "menu-item"})
|
||||||
|
for menu in soup.find_all(attrs={"data-test-id": "menu-item"}):
|
||||||
|
att=menu
|
||||||
|
#riempie la lista "nome"
|
||||||
|
for att in menu.find(attrs={"data-test-id": "menu-item-name"}):
|
||||||
|
if att != type(None):
|
||||||
|
if att != " ":
|
||||||
|
nome.append(att.lstrip().splitlines()[0])
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
#riempie la lista "desc"
|
||||||
#PARSER
|
att=menu.find("p", class_="c-menuItems-description")
|
||||||
with open('JEmenu.html', 'r') as f:
|
if att != None:
|
||||||
page = f.read()
|
for att in menu.find("p", class_="c-menuItems-description"):
|
||||||
|
desc.append(att.lstrip().splitlines()[0])
|
||||||
soup = BeautifulSoup(page, "html.parser")
|
|
||||||
|
|
||||||
|
|
||||||
#Stora nome ristorante
|
|
||||||
nrist = soup.title.text[8:-32]
|
|
||||||
|
|
||||||
#Stora telefono del ristorante
|
|
||||||
pattern = re.compile(r'allergenPhoneNumber')
|
|
||||||
script_tags = soup.find_all('script', string=pattern)
|
|
||||||
pattern = re.compile(r'"allergenPhoneNumber":"(\d+)"')
|
|
||||||
tel = re.search(pattern, script_tags[0].next)
|
|
||||||
if tel:
|
|
||||||
tel = tel.group(1)
|
|
||||||
|
|
||||||
#Stato ristorante
|
|
||||||
restaurant_is_open = menu = soup.find(attrs={"data-js-test":"order-status-wrapper"}).text
|
|
||||||
restaurant_is_open = restaurant_is_open.replace('\n', ' ')
|
|
||||||
regex = r" {4,}"
|
|
||||||
result = re.split(regex, restaurant_is_open)
|
|
||||||
|
|
||||||
#indirizzo ristorante
|
|
||||||
restaurant_address = soup.find(attrs={"data-js-test":"header-restaurantAddress"}).text
|
|
||||||
|
|
||||||
#cicla le schede prodotto
|
|
||||||
menu = soup.find(attrs={"data-test-id": "menu-item"})
|
|
||||||
for menu in soup.find_all(attrs={"data-test-id": "menu-item"}):
|
|
||||||
att=menu
|
|
||||||
#riempie la lista "nome"
|
|
||||||
for att in menu.find(attrs={"data-test-id": "menu-item-name"}):
|
|
||||||
if att != type(None):
|
|
||||||
if att != " ":
|
|
||||||
nome.append(att.lstrip().splitlines()[0])
|
|
||||||
break
|
|
||||||
else:
|
else:
|
||||||
|
desc.append(None)
|
||||||
|
|
||||||
|
#riempie la lista "prezzo"
|
||||||
|
for att in menu.find(attrs={"data-js-test": "menu-item-price"}):
|
||||||
|
#prezzo.append(att.lstrip().splitlines()[0])
|
||||||
|
counter=0
|
||||||
|
if att != " " and counter % 2 == 0:
|
||||||
|
prezzo.append(att.lstrip())
|
||||||
|
counter+=1
|
||||||
|
else:
|
||||||
|
counter+=1
|
||||||
|
continue
|
||||||
continue
|
continue
|
||||||
|
|
||||||
#riempie la lista "desc"
|
#riempie la lista "npezzi"
|
||||||
att=menu.find("p", class_="c-menuItems-description")
|
att=menu.find_all(attrs={"data-test-id": "menu-item-description"})
|
||||||
if att != None:
|
if att != None:
|
||||||
for att in menu.find("p", class_="c-menuItems-description"):
|
if menu.text.find("pezzo") > 0 or menu.text.find("pezzi") > 0:
|
||||||
desc.append(att.lstrip().splitlines()[0])
|
npezzi.append(menu.text.splitlines()[7].lstrip())
|
||||||
else:
|
else:
|
||||||
desc.append(None)
|
npezzi.append(None)
|
||||||
|
|
||||||
#riempie la lista "prezzo"
|
|
||||||
for att in menu.find(attrs={"data-js-test": "menu-item-price"}):
|
|
||||||
#prezzo.append(att.lstrip().splitlines()[0])
|
|
||||||
counter=0
|
|
||||||
if att != " " and counter % 2 == 0:
|
|
||||||
prezzo.append(att.lstrip())
|
|
||||||
counter+=1
|
|
||||||
else:
|
|
||||||
counter+=1
|
|
||||||
continue
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
#riempie la lista "npezzi"
|
|
||||||
att=menu.find_all(attrs={"data-test-id": "menu-item-description"})
|
driver=''
|
||||||
if att != None:
|
page = ''
|
||||||
if menu.text.find("pezzo") > 0 or menu.text.find("pezzi") > 0:
|
def scraper():
|
||||||
npezzi.append(menu.text.splitlines()[7].lstrip())
|
global driver
|
||||||
|
global page
|
||||||
|
global restaurant_url
|
||||||
|
driver = webdriver.Chrome()
|
||||||
|
driver.get(restaurant_url)
|
||||||
|
|
||||||
|
wait = WebDriverWait(driver, 16)
|
||||||
|
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "c-menuItems-price")))
|
||||||
|
|
||||||
|
page = driver.page_source
|
||||||
|
|
||||||
|
with open('JEmenu.html', 'w') as f:
|
||||||
|
f.write(page)
|
||||||
|
|
||||||
|
soup = ''
|
||||||
|
def parser():
|
||||||
|
global soup
|
||||||
|
with open('JEmenu.html', 'r') as f:
|
||||||
|
page = f.read()
|
||||||
|
|
||||||
|
soup = BeautifulSoup(page, "html.parser")
|
||||||
|
|
||||||
|
def parserdarubrica():
|
||||||
|
global parser
|
||||||
|
global stora_tutto
|
||||||
|
folder_path = ('./DATI_RUBRICA/')
|
||||||
|
for filename in os.listdir(folder_path):
|
||||||
|
if filename.startswith(scelta):
|
||||||
|
print(filename)
|
||||||
|
filename = filename.replace(' ', ' ')
|
||||||
|
print(filename)
|
||||||
|
shutil.copy (folder_path + filename, f'./JEmenu.html')
|
||||||
|
parser()
|
||||||
|
stora_tutto()
|
||||||
else:
|
else:
|
||||||
npezzi.append(None)
|
print ("numero inesistente")
|
||||||
continue
|
|
||||||
|
|
||||||
#Chiude chromium
|
|
||||||
driver.quit()
|
|
||||||
|
|
||||||
# #stampa liste
|
|
||||||
print("\n")
|
|
||||||
for x in range(len(nome)):
|
|
||||||
# print("\n")
|
|
||||||
print(nome[x])
|
|
||||||
print(desc[x])
|
|
||||||
print(npezzi[x])
|
|
||||||
print(prezzo[x])
|
|
||||||
|
|
||||||
#stampa info ristorante
|
|
||||||
print("-" * (len(desc)) + "\n")
|
|
||||||
print(nrist)
|
|
||||||
print(restaurant_address.strip())
|
|
||||||
print("Telefono:",tel,"\n")
|
|
||||||
|
|
||||||
doppione = ""
|
|
||||||
for i in range(len(result)):
|
|
||||||
if re.search(r"[a-zA-Z]", result[i]):
|
|
||||||
if (result[i]) == doppione:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
def stampa_liste():
|
||||||
|
print("\n")
|
||||||
|
for x in range(len(nome)):
|
||||||
|
# print("\n")
|
||||||
|
print(nome[x])
|
||||||
|
print(desc[x])
|
||||||
|
print(npezzi[x])
|
||||||
|
print(prezzo[x])
|
||||||
|
|
||||||
|
def stampa_info():
|
||||||
|
print("-" * (len(desc)) + "\n")
|
||||||
|
print(nrist)
|
||||||
|
print(restaurant_address.strip())
|
||||||
|
print("Telefono:",tel,"\n")
|
||||||
|
|
||||||
|
doppione = ""
|
||||||
|
for i in range(len(result)):
|
||||||
|
if re.search(r"[a-zA-Z]", result[i]):
|
||||||
|
if (result[i]) == doppione:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
print(result[i])
|
||||||
|
elif re.search(r"\d", result[i]):
|
||||||
|
print(result[i], result[i+1])
|
||||||
|
doppione = (result[i+1])
|
||||||
|
print("\n" + "-" * (len(desc))+ "\n")
|
||||||
|
|
||||||
|
def genera_prezzoN():
|
||||||
|
#Genera la lista prezzoN[] che è un clone di "prezzo[] ma con i valori float anzichè string"
|
||||||
|
prezzoN = prezzo.copy()
|
||||||
|
for i in range(len(prezzo)):
|
||||||
|
if "€" in prezzo[i]:
|
||||||
|
prezzoN[i] = float(prezzo[i].replace("€", "").replace(",", ".").replace("da ", ""))
|
||||||
|
elif "Non" in prezzo[i]:
|
||||||
|
prezzoN[i] = 0
|
||||||
|
prezzoN[i] = float(prezzoN[i])
|
||||||
else:
|
else:
|
||||||
print(result[i])
|
prezzoN[i] = 99999
|
||||||
elif re.search(r"\d", result[i]):
|
|
||||||
print(result[i], result[i+1])
|
|
||||||
doppione = (result[i+1])
|
|
||||||
print("\n" + "-" * (len(desc))+ "\n")
|
|
||||||
|
|
||||||
|
|
||||||
#Genera la lista prezzoN[] che è un clone di "prezzo[] ma con i valori float anzichè string"
|
|
||||||
prezzoN = prezzo.copy()
|
def salvainrubrica():
|
||||||
for i in range(len(prezzo)):
|
maxn=0
|
||||||
if "€" in prezzo[i]:
|
def trova_nuovo_numero():
|
||||||
prezzoN[i] = float(prezzo[i].replace("€", "").replace(",", ".").replace("da ", ""))
|
global maxn
|
||||||
elif "Non" in prezzo[i]:
|
file_list = os.listdir('./DATI_RUBRICA')
|
||||||
prezzoN[i] = 0
|
number_list = []
|
||||||
prezzoN[i] = float(prezzoN[i])
|
# Estare il numero
|
||||||
|
for file_name in file_list:
|
||||||
|
if file_name[0].isdigit():
|
||||||
|
number_list.append(int(file_name.split('-')[0]))
|
||||||
|
# trova il massimo
|
||||||
|
if number_list:
|
||||||
|
max_number = max(number_list)
|
||||||
|
maxn=(max_number + 1)
|
||||||
|
|
||||||
|
if os.path.exists('rubrica.txt'):
|
||||||
|
#SE LA RUBRICA ESISTE
|
||||||
|
with open('rubrica.txt', 'a+') as rubrica:
|
||||||
|
rubrica.seek(0)
|
||||||
|
data = rubrica.read()
|
||||||
|
if restaurant_url not in data:
|
||||||
|
#QUANDO IL RISTORATE NON E' PRESENTE IN RUBRICA
|
||||||
|
saveit = input('Vuoi salvare il ristorante in rubrica? [Y|N] ')
|
||||||
|
#PER DEBUG
|
||||||
|
#saveit = "y"
|
||||||
|
if saveit.upper() in ['YES', 'Y', 'SI', 'S']:
|
||||||
|
os.makedirs("DATI_RUBRICA", exist_ok=True)
|
||||||
|
trova_nuovo_numero()
|
||||||
|
nristmax=(str(maxn) + "-" + nrist + '.html')
|
||||||
|
shutil.move ('JEmenu.html', f'./DATI_RUBRICA/{nristmax}')
|
||||||
|
if data:
|
||||||
|
rubrica.write('\n')
|
||||||
|
rubrica.write(nristmax + '\n')
|
||||||
|
rubrica.write(nrist + '\n')
|
||||||
|
rubrica.write(restaurant_url + '\n')
|
||||||
|
else:
|
||||||
|
#QUANDO IL RISTORATE E' GIA' PRESENTE IN RUBRICA
|
||||||
|
os.remove("JEmenu.html")
|
||||||
|
else:
|
||||||
|
#QUANDO IL RISTORATE E' GIA' PRESENTE IN RUBRICA
|
||||||
|
os.remove("JEmenu.html")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
prezzoN[i] = 99999
|
#SE LA RUBRICA NON ESISTE
|
||||||
|
with open('rubrica.txt', 'a+') as rubrica:
|
||||||
|
rubrica.seek(0)
|
||||||
|
data = rubrica.read()
|
||||||
|
#CHIEDE SE SI VUOLE SALVARE ALTRIMENTI PULISCE
|
||||||
|
saveit = input('Vuoi salvare il ristorante in rubrica? [Y|N] ')
|
||||||
|
#PER DEBUG
|
||||||
|
#saveit = "y"
|
||||||
|
if saveit.upper() in ['YES', 'Y', 'SI', 'S']:
|
||||||
|
os.makedirs("DATI_RUBRICA", exist_ok=True)
|
||||||
|
nristmax=("1" + "-" + nrist + '.html')
|
||||||
|
shutil.move ('JEmenu.html', f'./DATI_RUBRICA/{nristmax}')
|
||||||
|
if data:
|
||||||
|
rubrica.write('\n')
|
||||||
|
rubrica.write(nristmax + '\n')
|
||||||
|
rubrica.write(nrist + '\n')
|
||||||
|
rubrica.write(restaurant_url + '\n')
|
||||||
|
else:
|
||||||
|
#PULISCE
|
||||||
|
os.remove("JEmenu.html")
|
||||||
|
os.remove("rubrica.txt")
|
||||||
|
|
||||||
|
#############################################################################################
|
||||||
|
#############################################################################################
|
||||||
|
#############################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
if os.path.exists('rubrica.txt') and os.path.exists('./DATI_RUBRICA'):
|
||||||
|
|
||||||
|
wr = input('\nVuoi vedere la rubrica? [Y|N] ')
|
||||||
|
|
||||||
|
if wr.upper() in ['YES', 'Y', 'SI', 'S']:
|
||||||
|
print( '\n', os.listdir('./DATI_RUBRICA'), '\n')
|
||||||
|
|
||||||
|
scelta = input("Scegli un numero esistente o premi Enter per metterre un link: ")
|
||||||
|
|
||||||
|
parserdarubrica()
|
||||||
|
|
||||||
|
else:
|
||||||
|
inputurl()
|
||||||
|
scraper()
|
||||||
|
parser()
|
||||||
|
stora_tutto()
|
||||||
|
driver.quit()
|
||||||
|
else:
|
||||||
|
inputurl()
|
||||||
|
scraper()
|
||||||
|
parser()
|
||||||
|
stora_tutto()
|
||||||
|
driver.quit()
|
||||||
|
|
||||||
|
stampa_liste()
|
||||||
|
stampa_info()
|
||||||
|
genera_prezzoN()
|
||||||
|
salvainrubrica()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# #PER DEBUG
|
# #PER DEBUG
|
||||||
@@ -223,68 +336,4 @@ for i in range(len(prezzo)):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#PROPORRE DI SALVARE IN RUBRICA
|
|
||||||
|
|
||||||
maxn=0
|
|
||||||
def trova_nuovo_numero():
|
|
||||||
global maxn
|
|
||||||
file_list = os.listdir('./DATI_RUBRICA')
|
|
||||||
number_list = []
|
|
||||||
# Estare il numero
|
|
||||||
for file_name in file_list:
|
|
||||||
if file_name[0].isdigit():
|
|
||||||
number_list.append(int(file_name.split('-')[0]))
|
|
||||||
# trova il massimo
|
|
||||||
if number_list:
|
|
||||||
max_number = max(number_list)
|
|
||||||
maxn=(max_number + 1)
|
|
||||||
|
|
||||||
if os.path.exists('rubrica.txt'):
|
|
||||||
#SE LA RUBRICA ESISTE
|
|
||||||
with open('rubrica.txt', 'a+') as rubrica:
|
|
||||||
rubrica.seek(0)
|
|
||||||
data = rubrica.read()
|
|
||||||
if restaurant_url not in data:
|
|
||||||
#QUANDO IL RISTORATE NON E' PRESENTE IN RUBRICA
|
|
||||||
saveit = input('Vuoi salvare il ristorante in rubrica? [Y|N] ')
|
|
||||||
#PER DEBUG
|
|
||||||
#saveit = "y"
|
|
||||||
if saveit.upper() in ['YES', 'Y', 'SI', 'S']:
|
|
||||||
os.makedirs("DATI_RUBRICA", exist_ok=True)
|
|
||||||
trova_nuovo_numero()
|
|
||||||
nristmax=(str(maxn) + "-" + nrist + '.html')
|
|
||||||
shutil.move ('JEmenu.html', f'./DATI_RUBRICA/{nristmax}')
|
|
||||||
if data:
|
|
||||||
rubrica.write('\n')
|
|
||||||
rubrica.write(nristmax + '\n')
|
|
||||||
rubrica.write(nrist + '\n')
|
|
||||||
rubrica.write(restaurant_url + '\n')
|
|
||||||
else:
|
|
||||||
#QUANDO IL RISTORATE E' GIA' PRESENTE IN RUBRICA
|
|
||||||
os.remove("JEmenu.html")
|
|
||||||
else:
|
|
||||||
#QUANDO IL RISTORATE E' GIA' PRESENTE IN RUBRICA
|
|
||||||
os.remove("JEmenu.html")
|
|
||||||
|
|
||||||
else:
|
|
||||||
#SE LA RUBRICA NON ESISTE
|
|
||||||
with open('rubrica.txt', 'a+') as rubrica:
|
|
||||||
rubrica.seek(0)
|
|
||||||
data = rubrica.read()
|
|
||||||
#CHIEDE SE SI VUOLE SALVARE ALTRIMENTI PULISCE
|
|
||||||
saveit = input('Vuoi salvare il ristorante in rubrica? [Y|N] ')
|
|
||||||
#PER DEBUG
|
|
||||||
#saveit = "y"
|
|
||||||
if saveit.upper() in ['YES', 'Y', 'SI', 'S']:
|
|
||||||
os.makedirs("DATI_RUBRICA", exist_ok=True)
|
|
||||||
nristmax=("1" + "-" + nrist + '.html')
|
|
||||||
shutil.move ('JEmenu.html', f'./DATI_RUBRICA/{nristmax}')
|
|
||||||
if data:
|
|
||||||
rubrica.write('\n')
|
|
||||||
rubrica.write(nristmax + '\n')
|
|
||||||
rubrica.write(nrist + '\n')
|
|
||||||
rubrica.write(restaurant_url + '\n')
|
|
||||||
else:
|
|
||||||
#PULISCE
|
|
||||||
os.remove("JEmenu.html")
|
|
||||||
os.remove("rubrica.txt")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user