Update 'scrapeje.py'
This commit is contained in:
137
scrapeje.py
Normal file
137
scrapeje.py
Normal file
@@ -0,0 +1,137 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
#import cloudscraper
|
||||
#import json
|
||||
import colorama
|
||||
from colorama import Fore
|
||||
from colorama import Style
|
||||
|
||||
nome=[]
|
||||
desc=[]
|
||||
npezzi=[]
|
||||
prezzo=[]
|
||||
scripto=[]
|
||||
|
||||
|
||||
#prende l url della pagina justeat del ristorante in input
|
||||
#url = "https://www.justeat.it/restaurants-pizzeria-girasole-bologna/menu"
|
||||
#url = input('link della pagina justeat del ristorante: ')
|
||||
#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("inserisci il link della pagina justeat del ristorante: ")
|
||||
|
||||
|
||||
#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
|
||||
|
||||
#crea il file html
|
||||
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
|
||||
|
||||
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")
|
||||
|
||||
#Stora nome ristorante
|
||||
nrist = soup.title.text[8:-32]
|
||||
|
||||
#Stora telefono del ristorante
|
||||
for i in soup.findAll('script'):
|
||||
scripto.append(i)
|
||||
|
||||
scriptok = scripto[7]
|
||||
scriptok = scriptok.string
|
||||
|
||||
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]
|
||||
|
||||
#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("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
|
||||
print("\n")
|
||||
print("ristorante:",nrist)
|
||||
print("telefono:",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)
|
||||
|
||||
|
||||
# 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")
|
||||
Reference in New Issue
Block a user