2023-11-26 21:28:34 +01:00
|
|
|
#IMPORTO MODULI
|
|
|
|
|
#selenium: scraper | bs4: parser | re: regex | os: interazione con os
|
2023-11-26 19:13:50 +01:00
|
|
|
from selenium import webdriver
|
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
|
from selenium.webdriver.common.by import By
|
2023-11-26 21:28:34 +01:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
import re
|
|
|
|
|
import os
|
2023-11-26 19:13:50 +01:00
|
|
|
|
|
|
|
|
#Inizializzo liste
|
|
|
|
|
nome=[]
|
|
|
|
|
desc=[]
|
|
|
|
|
npezzi=[]
|
|
|
|
|
prezzo=[]
|
|
|
|
|
prezzoN=[]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#INPUT
|
|
|
|
|
#prende l url della pagina justeat del ristorante in input
|
2023-11-26 21:29:26 +01:00
|
|
|
print ("\nesempio: https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu")
|
|
|
|
|
restaurant_url = input('INSERISCI IL LINK DELLA PAGINA DEL RISTORANTE: ')
|
2023-11-26 19:13:50 +01:00
|
|
|
|
|
|
|
|
#PER DEBUG
|
2023-11-26 21:28:34 +01:00
|
|
|
# restaurant_url = 'https://www.justeat.it/restaurants-pizzeria-del-mercato-bologna/menu'
|
2023-11-26 19:13:50 +01:00
|
|
|
|
|
|
|
|
|
2023-11-26 21:29:26 +01:00
|
|
|
#SCRAPE
|
|
|
|
|
driver = webdriver.Chrome()
|
|
|
|
|
driver.get(restaurant_url)
|
2023-11-26 19:13:50 +01:00
|
|
|
|
2023-11-26 21:29:26 +01:00
|
|
|
wait = WebDriverWait(driver, 10)
|
|
|
|
|
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "c-menuItems-price--offline")))
|
2023-11-26 19:13:50 +01:00
|
|
|
|
2023-11-26 21:29:26 +01:00
|
|
|
page = driver.page_source
|
2023-11-26 19:13:50 +01:00
|
|
|
|
2023-11-26 21:29:26 +01:00
|
|
|
with open('JEmenu.html', 'w') as f:
|
|
|
|
|
f.write(page)
|
2023-11-26 19:13:50 +01:00
|
|
|
|
2023-11-26 21:19:29 +01:00
|
|
|
|
|
|
|
|
#PARSER
|
2023-11-26 19:13:50 +01:00
|
|
|
with open('JEmenu.html', 'r') as f:
|
|
|
|
|
page = f.read()
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
#riempie la lista "desc"
|
|
|
|
|
att=menu.find("p", class_="c-menuItems-description")
|
|
|
|
|
if att != None:
|
|
|
|
|
for att in menu.find("p", class_="c-menuItems-description"):
|
|
|
|
|
desc.append(att.lstrip().splitlines()[0])
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
#riempie la lista "npezzi"
|
|
|
|
|
att=menu.find_all(attrs={"data-test-id": "menu-item-description"})
|
|
|
|
|
if att != None:
|
|
|
|
|
if menu.text.find("pezzo") > 0 or menu.text.find("pezzi") > 0:
|
|
|
|
|
npezzi.append(menu.text.splitlines()[7].lstrip())
|
|
|
|
|
else:
|
|
|
|
|
npezzi.append(None)
|
|
|
|
|
continue
|
|
|
|
|
|
2023-11-26 19:36:55 +01:00
|
|
|
|
2023-11-26 19:13:50 +01:00
|
|
|
# #stampa liste
|
2023-11-26 21:19:29 +01:00
|
|
|
print("\n")
|
2023-11-26 19:13:50 +01:00
|
|
|
for x in range(len(nome)):
|
2023-11-26 19:36:55 +01:00
|
|
|
# print("\n")
|
2023-11-26 19:13:50 +01:00
|
|
|
print(nome[x])
|
|
|
|
|
print(desc[x])
|
|
|
|
|
print(npezzi[x])
|
|
|
|
|
print(prezzo[x])
|
|
|
|
|
|
2023-11-26 19:42:20 +01:00
|
|
|
#stampa info ristorante
|
2023-11-26 21:19:29 +01:00
|
|
|
print("-" * (len(desc)) + "\n")
|
2023-11-26 19:13:50 +01:00
|
|
|
print(nrist)
|
2023-11-26 21:19:29 +01:00
|
|
|
print(restaurant_address.strip())
|
|
|
|
|
print("Telefono:",tel,"\n")
|
|
|
|
|
|
2023-11-26 19:13:50 +01:00
|
|
|
doppione = ""
|
|
|
|
|
for i in range(len(result)):
|
|
|
|
|
if re.search(r"[a-zA-Z]", result[i]):
|
|
|
|
|
if (result[i]) == doppione:
|
2023-11-26 19:36:55 +01:00
|
|
|
continue
|
2023-11-26 19:13:50 +01:00
|
|
|
else:
|
|
|
|
|
print(result[i])
|
|
|
|
|
elif re.search(r"\d", result[i]):
|
|
|
|
|
print(result[i], result[i+1])
|
|
|
|
|
doppione = (result[i+1])
|
2023-11-26 21:19:29 +01:00
|
|
|
print("\n" + "-" * (len(desc))+ "\n")
|
2023-11-26 19:36:55 +01:00
|
|
|
|
2023-11-26 19:13:50 +01:00
|
|
|
|
2023-11-26 21:19:29 +01:00
|
|
|
#Genera la lista prezzoN[] che è un clone di "prezzo[] ma con i valori float anzichè string"
|
2023-11-26 19:20:18 +01:00
|
|
|
prezzoN = prezzo.copy()
|
2023-11-26 19:13:50 +01:00
|
|
|
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:
|
|
|
|
|
prezzoN[i] = 99999
|
2023-11-26 19:36:55 +01:00
|
|
|
|
2023-11-26 19:13:50 +01:00
|
|
|
|
2023-11-26 21:19:29 +01:00
|
|
|
# #PER DEBUG
|
|
|
|
|
# for x in range(len(nome)):
|
|
|
|
|
# print(prezzoN[x])
|
|
|
|
|
# print("\n" + "-" * 25 + "\n")
|
|
|
|
|
# print ("lista prezzo: ",len(prezzo))
|
|
|
|
|
# print ("lista prezzoN: ",len(prezzoN))
|
|
|
|
|
# print ("lista nome: ",len(nome))
|
|
|
|
|
|
|
|
|
|
# #PER DEBUG
|
|
|
|
|
# #stampa lunghezza liste
|
|
|
|
|
# print("\n" + "-" * 25 + "\n")
|
|
|
|
|
# print("lista nome: ",len(nome))
|
|
|
|
|
# print("lista desc: ",len(desc))
|
|
|
|
|
# print("lista npezzi: ",len(npezzi))
|
|
|
|
|
# print("lista prezzi: ",len(prezzo)) #sono stringhe"
|
|
|
|
|
# print("\n" + "-" * 25 + "\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Genera codice HTML
|
|
|
|
|
# html = "<html><body>"
|
|
|
|
|
# for x in range(len(nome)):
|
|
|
|
|
# html += "<h2>" + nome[x] + "</h2>"
|
|
|
|
|
# html += "<p>" + str(desc[x]) + "</p>"
|
|
|
|
|
# html += "<p>N. pezzi disponibili: " + str(npezzi[x]) + "</p>"
|
|
|
|
|
# html += "<p>Prezzo: €" + str(prezzoN[x]) + "</p>"
|
|
|
|
|
# html += "<button onclick=\"aggiungiProdotto('" + nome[x] + "', '" + str(prezzoN[x]) + "')\">+1</button>"
|
|
|
|
|
# html += "<br><br>"
|
|
|
|
|
|
|
|
|
|
# html += "<br><hr><h2>Prodotti aggiunti</h2>"
|
|
|
|
|
# html += "<div id=\"prodottiAggiunti\"></div>"
|
|
|
|
|
# html += "<script>"
|
|
|
|
|
# html += "function aggiungiProdotto(nome, prezzoN) {"
|
|
|
|
|
# html += " var prodotto = nome + ' (€' + Number(prezzo).toFixed(2) + ')';"
|
|
|
|
|
# html += " var box = document.getElementById('prodottiAggiunti');"
|
|
|
|
|
# html += " box.innerHTML += '<p>' + prodotto + '</p>';"
|
|
|
|
|
# html += "}"
|
|
|
|
|
# html += "</script>"
|
|
|
|
|
# html += "</body></html>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Salvataggio su file
|
|
|
|
|
# with open("pagina.html", "w") as file:
|
|
|
|
|
# file.write(html)
|
|
|
|
|
|
2023-11-26 21:28:34 +01:00
|
|
|
|
2023-11-26 21:19:29 +01:00
|
|
|
#Pulisce
|
|
|
|
|
#ToDo:PROPORRE DI SALVARE IN RUBRICA
|
2023-11-26 21:29:26 +01:00
|
|
|
os.remove("JEmenu.html")
|