Files
scrapeje/ScrapeJE.py

138 lines
4.1 KiB
Python
Raw Normal View History

import requests
from bs4 import BeautifulSoup
2022-12-01 15:19:56 +01:00
#import cloudscraper
2022-12-01 15:30:55 +01:00
#import json
2022-12-01 23:26:10 +01:00
import colorama
from colorama import Fore
from colorama import Style
nome=[]
desc=[]
2022-09-29 19:07:26 +02:00
npezzi=[]
prezzo=[]
2022-12-01 15:19:56 +01:00
scripto=[]
2022-10-01 03:28:54 +02:00
#prende l url della pagina justeat del ristorante in input
2022-12-01 23:26:10 +01:00
#url = "https://www.justeat.it/restaurants-pizzeria-girasole-bologna/menu"
2022-12-01 15:19:56 +01:00
#url = input('link della pagina justeat del ristorante: ')
2022-12-01 23:26:10 +01:00
#input ('inserisci il link della pagina justeat del ristorante: ')
print("\n")
url = print(Fore.WHITE + Style.DIM + "es https://www.justeat.it/NOME_RISTORANTE/menu" + Style.RESET_ALL)
url = input(Fore.RED + Style.BRIGHT + "inserisci il link della pagina justeat del ristorante: " + Style.RESET_ALL)
2022-12-01 15:19:56 +01:00
#scrape html scavalcando cloudflare
# scraper = cloudscraper.create_scraper(browser={'browser': 'firefox','platform': 'windows','mobile': False})
# page = scraper.get(url).content #usa input manuale
# page = scraper.get("https://www.justeat.it/restaurants-saporedialeppo/menu").content #usa input automatico
2022-10-01 03:28:54 +02:00
#crea il file html
2022-12-01 15:19:56 +01:00
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
page = requests.get(url, headers=headers).content
2022-10-03 09:49:22 +02:00
with open('JEmenu.html', 'wb') as f:
2022-10-01 03:09:17 +02:00
f.write(page)
2022-10-01 03:28:54 +02:00
#apre e legge il file
2022-10-03 09:49:22 +02:00
with open('JEmenu.html', 'rb') as f:
page = f.read()
2022-10-01 03:28:54 +02:00
#parser
soup = BeautifulSoup(page, "html.parser")
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
2022-12-01 15:19:56 +01:00
#Stora telefono del ristorante
for i in soup.findAll('script'):
scripto.append(i)
2022-12-01 15:19:56 +01:00
scriptok = scripto[7]
scriptok = scriptok.string
2022-10-01 03:28:54 +02:00
2022-12-01 15:19:56 +01:00
say = 'allergenPhoneNumber":"'
after = scriptok[scriptok.index(say) + len(say):]
after = after[0:13]
after = ''.join((x for x in after if x.isdigit()))
tel = after[0:13]
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])
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 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
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-12-01 15:19:56 +01:00
#stampa lunghezza liste
2022-10-01 00:22:45 +02:00
print("\n")
2022-12-01 15:30:55 +01:00
print("ristorante:",nrist)
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))
2022-12-01 15:19:56 +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)
# 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")