2023-07-31 00:37:12 +02:00
|
|
|
import re
|
2022-09-27 18:47:25 +02:00
|
|
|
from bs4 import BeautifulSoup
|
2023-07-31 00:37:12 +02: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-25 20:38:36 +01:00
|
|
|
#import requests
|
|
|
|
|
#import cloudscraper
|
|
|
|
|
#import json
|
2023-07-31 00:37:12 +02:00
|
|
|
|
2022-09-27 18:47:25 +02:00
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#Inizializzo liste
|
2022-09-27 18:47:25 +02:00
|
|
|
nome=[]
|
|
|
|
|
desc=[]
|
2022-09-29 19:07:26 +02:00
|
|
|
npezzi=[]
|
2022-09-27 18:47:25 +02:00
|
|
|
prezzo=[]
|
2023-07-31 00:37:12 +02:00
|
|
|
prezzof=[]
|
2022-12-01 15:19:56 +01:00
|
|
|
scripto=[]
|
|
|
|
|
|
2022-09-27 18:47:25 +02:00
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#INPUT
|
2022-10-01 03:28:54 +02:00
|
|
|
#prende l url della pagina justeat del ristorante in input
|
2023-11-25 20:38:36 +01:00
|
|
|
#restaurant_url = input('link della pagina justeat del ristorante: ')
|
2022-10-01 03:28:54 +02:00
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#PER DEBUG
|
|
|
|
|
restaurant_url = 'https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu'
|
2023-07-31 00:37:12 +02:00
|
|
|
|
|
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#INIZIALLIZZO PARSER
|
2023-07-31 00:37:12 +02:00
|
|
|
driver = webdriver.Chrome()
|
|
|
|
|
driver.get(restaurant_url)
|
|
|
|
|
|
|
|
|
|
wait = WebDriverWait(driver, 10)
|
|
|
|
|
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "c-menuItems-price--offline")))
|
|
|
|
|
|
|
|
|
|
page = driver.page_source
|
|
|
|
|
|
|
|
|
|
with open('JEmenu.html', 'w') as f:
|
2022-10-01 03:09:17 +02:00
|
|
|
f.write(page)
|
2022-09-27 18:47:25 +02:00
|
|
|
|
2022-10-01 03:28:54 +02:00
|
|
|
#apre e legge il file
|
2023-07-31 00:37:12 +02:00
|
|
|
with open('JEmenu.html', 'r') as f:
|
2022-09-27 18:47:25 +02:00
|
|
|
page = f.read()
|
|
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#PARSER
|
2022-09-27 18:47:25 +02:00
|
|
|
soup = BeautifulSoup(page, "html.parser")
|
2022-10-01 00:22:45 +02:00
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
|
2022-10-01 00:22:45 +02:00
|
|
|
#Stora nome ristorante
|
2022-12-01 15:19:56 +01:00
|
|
|
nrist = soup.title.text[8:-32]
|
2022-09-30 23:27:10 +02:00
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#Stora telefono del ristorante
|
2023-07-31 00:37:12 +02:00
|
|
|
pattern = re.compile(r'allergenPhoneNumber')
|
2023-11-25 20:38:36 +01:00
|
|
|
script_tags = soup.find_all('script', string=pattern)
|
2023-07-31 00:37:12 +02:00
|
|
|
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)
|
|
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#indirizzo ristorante
|
2023-07-31 00:37:12 +02:00
|
|
|
restaurant_address = soup.find(attrs={"data-js-test":"header-restaurantAddress"}).text
|
2022-10-01 03:28:54 +02:00
|
|
|
|
|
|
|
|
#cicla le schede prodotto
|
2022-12-01 15:19:56 +01:00
|
|
|
menu = soup.find(attrs={"data-test-id": "menu-item"})
|
2022-09-29 19:07:26 +02:00
|
|
|
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"}):
|
2022-12-01 23:26:10 +01:00
|
|
|
if att != type(None):
|
|
|
|
|
if att != " ":
|
|
|
|
|
nome.append(att.lstrip().splitlines()[0])
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
|
2022-09-29 19:07:26 +02:00
|
|
|
#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])
|
2022-09-27 18:47:25 +02:00
|
|
|
else:
|
2022-09-29 19:07:26 +02:00
|
|
|
desc.append(None)
|
2022-09-27 18:47:25 +02:00
|
|
|
|
|
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
#riempie la lista "prezzo"
|
|
|
|
|
#for att in menu.find(attrs={"data-js-test": "menu-item-price"}):
|
|
|
|
|
# prezzo.append(att.lstrip().splitlines()[0])
|
2023-07-31 00:37:12 +02:00
|
|
|
|
|
|
|
|
|
2022-10-01 03:28:54 +02:00
|
|
|
#riempie la lista "npezzi"
|
2022-09-30 23:27:10 +02:00
|
|
|
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-25 20:38:36 +01:00
|
|
|
|
2023-07-31 00:37:12 +02:00
|
|
|
# #stampa liste
|
2022-09-27 18:47:25 +02:00
|
|
|
for x in range(len(nome)):
|
2022-09-30 23:27:10 +02:00
|
|
|
print("\n")
|
|
|
|
|
print(nome[x])
|
|
|
|
|
print(desc[x])
|
|
|
|
|
print(npezzi[x])
|
2023-11-25 20:38:36 +01:00
|
|
|
# print(prezzo[x])
|
2023-07-31 00:37:12 +02:00
|
|
|
regex = r"(?:da\s+)?([\d.]+)"
|
|
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
# match = re.search(regex, prezzo[x])
|
|
|
|
|
# if match:
|
|
|
|
|
# numero = match.group(1)
|
|
|
|
|
# print(numero)
|
|
|
|
|
# prezzof.append(int(numero))
|
|
|
|
|
# else:
|
|
|
|
|
# prezzof.append(0)
|
|
|
|
|
# print(prezzof[x])
|
|
|
|
|
|
2022-09-29 19:07:26 +02:00
|
|
|
|
2022-12-01 15:19:56 +01:00
|
|
|
#stampa lunghezza liste
|
2022-10-01 00:22:45 +02:00
|
|
|
print("\n")
|
2023-07-31 00:37:12 +02:00
|
|
|
print(nrist)
|
|
|
|
|
print(restaurant_address.strip())
|
|
|
|
|
print("\n")
|
|
|
|
|
doppione = ""
|
|
|
|
|
for i in range(len(result)):
|
|
|
|
|
if re.search(r"[a-zA-Z]", result[i]):
|
|
|
|
|
if (result[i]) == doppione:
|
|
|
|
|
print("\n")
|
|
|
|
|
else:
|
|
|
|
|
print(result[i])
|
|
|
|
|
elif re.search(r"\d", result[i]):
|
|
|
|
|
print(result[i], result[i+1])
|
|
|
|
|
doppione = (result[i+1])
|
2022-12-01 15:30:55 +01:00
|
|
|
print("telefono:",tel)
|
|
|
|
|
print("lista nome:",len(nome))
|
2022-09-29 19:07:26 +02:00
|
|
|
print("lista desc:",len(desc))
|
|
|
|
|
print("lista npezzi:",len(npezzi))
|
2023-11-25 20:38:36 +01:00
|
|
|
#print("lista prezzi:",len(prezzo)) #sono stringhe ovvero ci sono anche prezzi come "da 1,00 €" (servirà formattarla in double per poter fare i conti)
|
2023-07-31 00:37:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Generazione del codice HTML
|
|
|
|
|
html = "<html><body>"
|
|
|
|
|
for x in range(len(nome)):
|
|
|
|
|
html += "<h2>" + nome[x] + "</h2>"
|
2023-11-25 20:38:36 +01:00
|
|
|
# html += "<p>" + desc[x] + "</p>"
|
2023-07-31 00:37:12 +02:00
|
|
|
html += "<p>N. pezzi disponibili: " + str(npezzi[x]) + "</p>"
|
2023-11-25 20:38:36 +01:00
|
|
|
# html += "<p>Prezzo: €" + str(prezzo[x]) + "</p>"
|
|
|
|
|
# html += "<button onclick=\"aggiungiProdotto('" + nome[x] + "', '" + str(prezzof[x]) + "')\">+1</button>"
|
2023-07-31 00:37:12 +02:00
|
|
|
html += "<br><br>"
|
|
|
|
|
|
|
|
|
|
html += "<br><hr><h2>Prodotti aggiunti</h2>"
|
|
|
|
|
html += "<div id=\"prodottiAggiunti\"></div>"
|
|
|
|
|
html += "<script>"
|
|
|
|
|
html += "function aggiungiProdotto(nome, prezzo) {"
|
|
|
|
|
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>"
|
|
|
|
|
|
2023-11-25 20:38:36 +01:00
|
|
|
|
2023-07-31 00:37:12 +02:00
|
|
|
# Salvataggio su file
|
|
|
|
|
with open("pagina.html", "w") as file:
|
|
|
|
|
file.write(html)
|