127 lines
3.8 KiB
Python
127 lines
3.8 KiB
Python
import requests
|
|
from bs4 import BeautifulSoup
|
|
import cloudscraper
|
|
import json
|
|
|
|
nome=[]
|
|
desc=[]
|
|
npezzi=[]
|
|
prezzo=[]
|
|
|
|
#prende l url della pagina justeat del ristorante in input
|
|
#linkJE = input('link della pagina justeat del ristorante: ') #decommenta per input manuale
|
|
|
|
#scrape html
|
|
scraper = cloudscraper.create_scraper(browser={'browser': 'firefox','platform': 'windows','mobile': False})
|
|
#page = scraper.get(linkJE).content #usa input manuale
|
|
page = scraper.get("https://www.justeat.it/restaurants-saporedialeppo/menu").content #usa input automatico
|
|
#crea il file html
|
|
with open('JEmenu.html', 'wb') as f:
|
|
f.write(page)
|
|
|
|
#apre e legge il file
|
|
with open('JEmenu.html', 'rb') as f:
|
|
page = f.read()
|
|
|
|
#parser
|
|
soup = BeautifulSoup(page, "html.parser")
|
|
menu = soup.find(attrs={"data-test-id": "menu-item"})
|
|
|
|
|
|
#Stora nome ristorante
|
|
nrist=soup.title.text[8:-32]
|
|
menu.find(attrs={"allergenPhoneNumber": "menu-item-name"})
|
|
|
|
|
|
##
|
|
#Stora il numero di telfono del ristorante
|
|
#info-> alla riga 870 dell html, all interno di uno <script> c'è il numero in forma-> "allergenPhoneNumber":"3389529446" (es riferito a quando si scrapa aleppo)
|
|
#tel=
|
|
##
|
|
|
|
|
|
#cicla le schede prodotto
|
|
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 != " ":
|
|
nome.append(att.lstrip().splitlines()[0])
|
|
|
|
#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("p", class_="c-menuItems-price notranslate"):
|
|
prezzo.append(att.lstrip().splitlines()[0])
|
|
|
|
#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
|
|
|
|
#stampa liste
|
|
for x in range(len(nome)):
|
|
print("\n")
|
|
print(nome[x])
|
|
print(desc[x])
|
|
print(npezzi[x])
|
|
print(prezzo[x])
|
|
|
|
#stampa lunghezza liste e nome del risrorante # e numero di telefono
|
|
print("\n")
|
|
print(nrist)
|
|
#print(tel)
|
|
print("lista nome",len(nome))
|
|
print("lista desc:",len(desc))
|
|
print("lista npezzi:",len(npezzi))
|
|
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 a fine doodle)
|
|
|
|
|
|
#Crea file json formattato per jawanndenn con la lista dei nomi dei prodotti
|
|
#scrive le prime righe del json (uguali ogni volta)
|
|
with open('jwndn.json', 'w') as jw:
|
|
jw.write("{\n")
|
|
jw.write(' "lifetime": "month",\n')
|
|
jw.write(' "equal_width": true,\n')
|
|
jw.write(' "title":')
|
|
jw.write('"')
|
|
jw.write(nrist)
|
|
jw.write('",')
|
|
jw.write("\n")
|
|
jw.write(' "options": \n')
|
|
|
|
#inserisce i nomi dei prodotti dalla lista
|
|
def writeListJSONFile(filepathname, lista):
|
|
with open('./jwndn.json', 'a+') as f:
|
|
json.dump(nome, f)
|
|
|
|
writeListJSONFile('./jwndn.json', nome)
|
|
|
|
#conclude il json con l ultima parentesi graffa
|
|
with open('jwndn.json', 'a+') as jw:
|
|
jw.write("\n}\n")
|
|
|
|
|
|
|
|
#
|
|
#storare in "tel" il numero di telefono dle ristorante
|
|
#automatizzare l inserimento in jawandenn
|
|
|
|
#PS jawanndenn include la funzione per argomento
|
|
#"jawanndenn --loaddata FILE.json" --> Load a JSON export of the database from FILE.json, then quit.
|
|
|
|
#aggiungere la possibilità di mettere piu di un voto al doodle (ovvero prendere piu prodotti come es: 2 fatayer al formaggio)
|
|
|
|
#fare in modo che quando si passa il mouse sul nome del prodotto (nel doodle) compaiano descrizione, npezzi e prezzo
|
|
#forse per il prezzo trattamento diverso
|
|
# |