Files
scrapeje/ScrapeJE.py

79 lines
2.3 KiB
Python
Raw Normal View History

import requests
from bs4 import BeautifulSoup
import cloudscraper
nome=[]
desc=[]
2022-09-29 19:07:26 +02:00
npezzi=[]
prezzo=[]
2022-10-01 00:22:45 +02:00
n=0
2022-10-01 03:09:17 +02:00
linkJE = input('Incolla il link del ristorante: ')
linkJE
#scrape html
2022-10-01 03:09:17 +02:00
scraper = cloudscraper.create_scraper(browser={'browser': 'firefox','platform': 'windows','mobile': False})
page = scraper.get(linkJE).content
with open('aleppo.html', 'wb') as f:
f.write(page)
with open('aleppo.html', 'rb') as f:
page = f.read()
soup = BeautifulSoup(page, "html.parser")
2022-09-29 19:07:26 +02:00
menu = soup.find(attrs={"data-test-id": "menu-item"})
2022-10-01 00:22:45 +02:00
#Stora nome ristorante
2022-10-01 03:09:17 +02:00
nrist=soup.title.text[8:-32]
2022-10-01 00:22:45 +02:00
menu.find(attrs={"allergenPhoneNumber": "menu-item-name"})
2022-09-30 23:27:10 +02:00
# alla riga 870 dell html c'è "allergenPhoneNumber":"3389529446"
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"}):
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:
2022-09-29 19:07:26 +02:00
desc.append(None)
2022-09-29 19:07:26 +02:00
#riempie la lista "prezzo"
for att in menu.find("p", class_="c-menuItems-price notranslate"):
prezzo.append(att.lstrip().splitlines()[0])
2022-10-01 00:22:45 +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
2022-09-29 19:07:26 +02:00
#stampa liste
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])
print(prezzo[x])
2022-09-29 19:07:26 +02:00
2022-10-01 00:22:45 +02:00
#stampa lunghezza liste del risrorante
print("\n")
print(nrist)
print("lista nome",len(nome),)
2022-09-29 19:07:26 +02:00
print("lista desc:",len(desc))
print("lista npezzi:",len(npezzi))
2022-10-01 03:09:17 +02:00
print("lista prezzi:",len(prezzo)) #da trattare come stringa perchè ci sono anche i es. "da 1,00 €"
#Creare un file json formattato per jawanndenn
#automatizzare l inserimento in jawandenn
#jawanndenn include la funzione per argomento
#"jawanndenn --loaddata FILE.json" --> Load a JSON export of the database from FILE.json, then quit.