0..0.1
This commit is contained in:
153
scrapeje.py
153
scrapeje.py
@@ -1,15 +1,19 @@
|
||||
import requests
|
||||
import re
|
||||
#import requests
|
||||
from bs4 import BeautifulSoup
|
||||
#import cloudscraper
|
||||
#import json
|
||||
import colorama
|
||||
from colorama import Fore
|
||||
from colorama import Style
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
from selenium.webdriver.common.by import By
|
||||
|
||||
|
||||
nome=[]
|
||||
desc=[]
|
||||
npezzi=[]
|
||||
prezzo=[]
|
||||
prezzof=[]
|
||||
scripto=[]
|
||||
|
||||
|
||||
@@ -18,9 +22,9 @@ scripto=[]
|
||||
#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: ")
|
||||
# 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
|
||||
@@ -29,14 +33,32 @@ url = input("inserisci il link della pagina justeat del ristorante: ")
|
||||
# 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
|
||||
# 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:
|
||||
|
||||
restaurant_url = "https://www.justeat.it/restaurants-saporedialeppo/menu"
|
||||
restaurant_url = 'https://www.justeat.it/restaurants-gelateria-ice-cream-casalecchio-di-reno-40033/menu'
|
||||
restaurant_url = 'https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu'
|
||||
#restaurant_url = 'https://www.justeat.it/restaurants-viavaipizzaekebab/menu'
|
||||
driver = webdriver.Chrome()
|
||||
driver.get(restaurant_url)
|
||||
|
||||
wait = WebDriverWait(driver, 10)
|
||||
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "c-menuItems-price--offline")))
|
||||
|
||||
|
||||
|
||||
page = driver.page_source
|
||||
|
||||
|
||||
|
||||
|
||||
with open('JEmenu.html', 'w') as f:
|
||||
f.write(page)
|
||||
|
||||
#apre e legge il file
|
||||
with open('JEmenu.html', 'rb') as f:
|
||||
with open('JEmenu.html', 'r') as f:
|
||||
page = f.read()
|
||||
|
||||
#parser
|
||||
@@ -45,19 +67,35 @@ 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]
|
||||
|
||||
# #Stora telefono del ristorante
|
||||
|
||||
# Define the regex pattern
|
||||
pattern = re.compile(r'allergenPhoneNumber')
|
||||
|
||||
# Find all script tags that match the regex pattern
|
||||
script_tags = soup.find_all('script', text=pattern)
|
||||
|
||||
# Define the regex pattern
|
||||
pattern = re.compile(r'"allergenPhoneNumber":"(\d+)"')
|
||||
|
||||
# Search for the phone number in the given string
|
||||
tel = re.search(pattern, script_tags[0].next)
|
||||
if tel:
|
||||
tel = tel.group(1)
|
||||
|
||||
|
||||
#Stato ristorante
|
||||
restaurant_is_open = menu = soup.find(attrs={"data-js-test":"order-status-wrapper"}).text
|
||||
restaurant_is_open = restaurant_is_open.replace('\n', ' ')
|
||||
|
||||
regex = r" {4,}"
|
||||
result = re.split(regex, restaurant_is_open)
|
||||
|
||||
restaurant_address = soup.find(attrs={"data-js-test":"header-restaurantAddress"}).text
|
||||
|
||||
#cicla le schede prodotto
|
||||
menu = soup.find(attrs={"data-test-id": "menu-item"})
|
||||
@@ -82,9 +120,11 @@ for menu in soup.find_all(attrs={"data-test-id": "menu-item"}):
|
||||
desc.append(None)
|
||||
|
||||
#riempie la lista "prezzo"
|
||||
for att in menu.find("p", class_="c-menuItems-price notranslate"):
|
||||
for att in menu.find(attrs={"data-js-test": "menu-item-price"}):
|
||||
prezzo.append(att.lstrip().splitlines()[0])
|
||||
|
||||
|
||||
|
||||
#riempie la lista "npezzi"
|
||||
att=menu.find_all(attrs={"data-test-id": "menu-item-description"})
|
||||
if att != None:
|
||||
@@ -94,17 +134,39 @@ for menu in soup.find_all(attrs={"data-test-id": "menu-item"}):
|
||||
npezzi.append(None)
|
||||
continue
|
||||
|
||||
#stampa liste
|
||||
# #stampa liste
|
||||
for x in range(len(nome)):
|
||||
print("\n")
|
||||
print(nome[x])
|
||||
print(desc[x])
|
||||
print(npezzi[x])
|
||||
print(prezzo[x])
|
||||
regex = r"(?:da\s+)?([\d.]+)"
|
||||
|
||||
match = re.search(regex, prezzo[x])
|
||||
if match:
|
||||
numero = match.group(1)
|
||||
print(numero)
|
||||
prezzof.append(int(numero))
|
||||
else:
|
||||
prezzof.append(0)
|
||||
print(prezzof[x])
|
||||
|
||||
#stampa lunghezza liste
|
||||
print("\n")
|
||||
print("ristorante:",nrist)
|
||||
print(nrist)
|
||||
print(restaurant_address.strip())
|
||||
print("\n")
|
||||
doppione = ""
|
||||
for i in range(len(result)):
|
||||
if re.search(r"[a-zA-Z]", result[i]):
|
||||
if (result[i]) == doppione:
|
||||
print("\n")
|
||||
else:
|
||||
print(result[i])
|
||||
elif re.search(r"\d", result[i]):
|
||||
print(result[i], result[i+1])
|
||||
doppione = (result[i+1])
|
||||
print("telefono:",tel)
|
||||
print("lista nome:",len(nome))
|
||||
print("lista desc:",len(desc))
|
||||
@@ -112,26 +174,29 @@ 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)
|
||||
# Generazione del codice HTML
|
||||
html = "<html><body>"
|
||||
for x in range(len(nome)):
|
||||
html += "<h2>" + nome[x] + "</h2>"
|
||||
# html += "<p>" + desc[x] + "</p>"
|
||||
html += "<p>N. pezzi disponibili: " + str(npezzi[x]) + "</p>"
|
||||
html += "<p>Prezzo: €" + str(prezzo[x]) + "</p>"
|
||||
html += "<button onclick=\"aggiungiProdotto('" + nome[x] + "', '" + str(prezzof[x]) + "')\">+1</button>"
|
||||
html += "<br><br>"
|
||||
|
||||
# conclude il json con l ultima parentesi graffa
|
||||
# with open('jwndn.json', 'a+') as jw:
|
||||
# jw.write("\n}\n")
|
||||
html += "<br><hr><h2>Prodotti aggiunti</h2>"
|
||||
html += "<div id=\"prodottiAggiunti\"></div>"
|
||||
html += "<script>"
|
||||
html += "function aggiungiProdotto(nome, prezzo) {"
|
||||
html += " var prodotto = nome + ' (€' + Number(prezzo).toFixed(2) + ')';"
|
||||
html += " var box = document.getElementById('prodottiAggiunti');"
|
||||
html += " box.innerHTML += '<p>' + prodotto + '</p>';"
|
||||
html += "}"
|
||||
html += "</script>"
|
||||
html += "</body></html>"
|
||||
|
||||
# Salvataggio su file
|
||||
with open("pagina.html", "w") as file:
|
||||
file.write(html)
|
||||
Reference in New Issue
Block a user