Update 'ScrapeJE.py'
This commit is contained in:
102
ScrapeJE.py
102
ScrapeJE.py
@@ -1,21 +1,28 @@
|
|||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import cloudscraper
|
#import cloudscraper
|
||||||
import json
|
import json
|
||||||
|
|
||||||
nome=[]
|
nome=[]
|
||||||
desc=[]
|
desc=[]
|
||||||
npezzi=[]
|
npezzi=[]
|
||||||
prezzo=[]
|
prezzo=[]
|
||||||
|
scripto=[]
|
||||||
|
|
||||||
|
|
||||||
#prende l url della pagina justeat del ristorante in input
|
#prende l url della pagina justeat del ristorante in input
|
||||||
#linkJE = input('link della pagina justeat del ristorante: ') #decommenta per input manuale
|
url = "https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu"
|
||||||
|
#url = input('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
|
||||||
|
|
||||||
#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
|
#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:
|
with open('JEmenu.html', 'wb') as f:
|
||||||
f.write(page)
|
f.write(page)
|
||||||
|
|
||||||
@@ -25,22 +32,26 @@ with open('JEmenu.html', 'rb') as f:
|
|||||||
|
|
||||||
#parser
|
#parser
|
||||||
soup = BeautifulSoup(page, "html.parser")
|
soup = BeautifulSoup(page, "html.parser")
|
||||||
menu = soup.find(attrs={"data-test-id": "menu-item"})
|
|
||||||
|
|
||||||
|
|
||||||
#Stora nome ristorante
|
#Stora nome ristorante
|
||||||
nrist=soup.title.text[8:-32]
|
nrist = soup.title.text[8:-32]
|
||||||
menu.find(attrs={"allergenPhoneNumber": "menu-item-name"})
|
|
||||||
|
|
||||||
|
#Stora telefono del ristorante
|
||||||
|
for i in soup.findAll('script'):
|
||||||
|
scripto.append(i)
|
||||||
|
|
||||||
##
|
scriptok = scripto[7]
|
||||||
#Stora il numero di telfono del ristorante
|
scriptok = scriptok.string
|
||||||
#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=
|
|
||||||
##
|
|
||||||
|
|
||||||
|
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
|
#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"}):
|
for menu in soup.find_all(attrs={"data-test-id": "menu-item"}):
|
||||||
att=menu
|
att=menu
|
||||||
#riempie la lista "nome"
|
#riempie la lista "nome"
|
||||||
@@ -77,51 +88,36 @@ for x in range(len(nome)):
|
|||||||
print(npezzi[x])
|
print(npezzi[x])
|
||||||
print(prezzo[x])
|
print(prezzo[x])
|
||||||
|
|
||||||
#stampa lunghezza liste e nome del risrorante # e numero di telefono
|
#stampa lunghezza liste
|
||||||
print("\n")
|
print("\n")
|
||||||
print(nrist)
|
print(nrist)
|
||||||
#print(tel)
|
print(tel)
|
||||||
print("lista nome",len(nome))
|
print("lista nome",len(nome))
|
||||||
print("lista desc:",len(desc))
|
print("lista desc:",len(desc))
|
||||||
print("lista npezzi:",len(npezzi))
|
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)
|
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
|
# Crea file json formattato per jawanndenn con la lista dei nomi dei prodotti
|
||||||
#scrive le prime righe del json (uguali ogni volta)
|
# scrive le prime righe del json (uguali ogni volta)
|
||||||
with open('jwndn.json', 'w') as jw:
|
# with open('jwndn.json', 'w') as jw:
|
||||||
jw.write("{\n")
|
# jw.write("{\n")
|
||||||
jw.write(' "lifetime": "month",\n')
|
# jw.write(' "lifetime": "month",\n')
|
||||||
jw.write(' "equal_width": true,\n')
|
# jw.write(' "equal_width": true,\n')
|
||||||
jw.write(' "title":')
|
# jw.write(' "title":')
|
||||||
jw.write('"')
|
# jw.write('"')
|
||||||
jw.write(nrist)
|
# jw.write(nrist)
|
||||||
jw.write('",')
|
# jw.write('",')
|
||||||
jw.write("\n")
|
# jw.write("\n")
|
||||||
jw.write(' "options": \n')
|
# jw.write(' "options": \n')
|
||||||
|
|
||||||
#inserisce i nomi dei prodotti dalla lista
|
# inserisce i nomi dei prodotti dalla lista
|
||||||
def writeListJSONFile(filepathname, lista):
|
# def writeListJSONFile(filepathname, lista):
|
||||||
with open('./jwndn.json', 'a+') as f:
|
# with open('./jwndn.json', 'a+') as f:
|
||||||
json.dump(nome, f)
|
# json.dump(nome, f)
|
||||||
|
|
||||||
writeListJSONFile('./jwndn.json', nome)
|
# writeListJSONFile('./jwndn.json', nome)
|
||||||
|
|
||||||
#conclude il json con l ultima parentesi graffa
|
# conclude il json con l ultima parentesi graffa
|
||||||
with open('jwndn.json', 'a+') as jw:
|
# with open('jwndn.json', 'a+') as jw:
|
||||||
jw.write("\n}\n")
|
# 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
|
|
||||||
#
|
|
||||||
|
|||||||
Reference in New Issue
Block a user