update
This commit is contained in:
112
scrapeje.py
112
scrapeje.py
@@ -1,5 +1,5 @@
|
|||||||
#IMPORTO MODULI
|
#IMPORTO MODULI
|
||||||
#selenium: scraper | bs4: parser | re: regex | os: interazione con os
|
#selenium: scraper | bs4: parser | re: regex | os: interazione con os | shutil: interazione con la shell
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
@@ -7,6 +7,7 @@ from selenium.webdriver.common.by import By
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
#Inizializzo liste
|
#Inizializzo liste
|
||||||
nome=[]
|
nome=[]
|
||||||
@@ -16,21 +17,45 @@ prezzo=[]
|
|||||||
prezzoN=[]
|
prezzoN=[]
|
||||||
|
|
||||||
|
|
||||||
|
restaurant_url=''
|
||||||
|
def inputurl():
|
||||||
|
global restaurant_url
|
||||||
#INPUT
|
#INPUT
|
||||||
#prende l url della pagina justeat del ristorante in input
|
#prende l url della pagina justeat del ristorante in input
|
||||||
print ("\nesempio: https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu")
|
# print ("\nesempio: https://www.justeat.it/restaurants-pizzeria-la-garganica-bologna/menu")
|
||||||
restaurant_url = input('INSERISCI IL LINK DELLA PAGINA DEL RISTORANTE: ')
|
# restaurant_url = input('INSERISCI IL LINK DELLA PAGINA DEL RISTORANTE: ')
|
||||||
|
|
||||||
#PER DEBUG
|
#PER DEBUG
|
||||||
# restaurant_url = 'https://www.justeat.it/restaurants-pizzeria-del-mercato-bologna/menu'
|
restaurant_url = 'https://www.justeat.it/restaurants-dolceirnerio/menu'
|
||||||
|
|
||||||
|
|
||||||
|
# if os.path.exists('rubrica.txt'):
|
||||||
|
# if os.path.exists('./DATI_RUBRICA'):
|
||||||
|
|
||||||
|
# wr = input('\nVuoi vedere la rubrica? [Y|N] ')
|
||||||
|
# if wr.upper() in ['YES', 'Y', 'SI', 'S']:
|
||||||
|
# print( '\n', os.listdir('./DATI_RUBRICA'), '\n')
|
||||||
|
# scelta = input("Scegli un numero esistente o premi Enter per metterre un link: ")
|
||||||
|
# files = os.listdir('./DATI_RUBRICA/')
|
||||||
|
# for file in files:
|
||||||
|
# if scelta in file:
|
||||||
|
# restaurant_url = (file)
|
||||||
|
# else:
|
||||||
|
# print ("numero inesistente")
|
||||||
|
# inputurl()
|
||||||
|
# else:
|
||||||
|
# inputurl()
|
||||||
|
# else:
|
||||||
|
# inputurl()
|
||||||
|
# else:
|
||||||
|
# inputurl()
|
||||||
|
|
||||||
|
|
||||||
#SCRAPE
|
#SCRAPE
|
||||||
driver = webdriver.Chrome()
|
driver = webdriver.Chrome()
|
||||||
driver.get(restaurant_url)
|
driver.get(restaurant_url)
|
||||||
|
|
||||||
wait = WebDriverWait(driver, 10)
|
wait = WebDriverWait(driver, 16)
|
||||||
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "c-menuItems-price--offline")))
|
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "c-menuItems-price")))
|
||||||
|
|
||||||
page = driver.page_source
|
page = driver.page_source
|
||||||
|
|
||||||
@@ -107,6 +132,8 @@ for menu in soup.find_all(attrs={"data-test-id": "menu-item"}):
|
|||||||
npezzi.append(None)
|
npezzi.append(None)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
#Chiude chromium
|
||||||
|
driver.quit()
|
||||||
|
|
||||||
# #stampa liste
|
# #stampa liste
|
||||||
print("\n")
|
print("\n")
|
||||||
@@ -155,10 +182,11 @@ for i in range(len(prezzo)):
|
|||||||
# print ("lista prezzo: ",len(prezzo))
|
# print ("lista prezzo: ",len(prezzo))
|
||||||
# print ("lista prezzoN: ",len(prezzoN))
|
# print ("lista prezzoN: ",len(prezzoN))
|
||||||
# print ("lista nome: ",len(nome))
|
# print ("lista nome: ",len(nome))
|
||||||
|
# print("\n" + "-" * 25 + "\n")
|
||||||
|
|
||||||
|
|
||||||
# #PER DEBUG
|
# #PER DEBUG
|
||||||
# #stampa lunghezza liste
|
# #stampa lunghezza liste
|
||||||
# print("\n" + "-" * 25 + "\n")
|
|
||||||
# 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))
|
||||||
@@ -193,6 +221,70 @@ for i in range(len(prezzo)):
|
|||||||
# file.write(html)
|
# file.write(html)
|
||||||
|
|
||||||
|
|
||||||
#Pulisce
|
|
||||||
#ToDo:PROPORRE DI SALVARE IN RUBRICA
|
|
||||||
|
#PROPORRE DI SALVARE IN RUBRICA
|
||||||
|
|
||||||
|
maxn=0
|
||||||
|
def trova_nuovo_numero():
|
||||||
|
global maxn
|
||||||
|
file_list = os.listdir('./DATI_RUBRICA')
|
||||||
|
number_list = []
|
||||||
|
# Estare il numero
|
||||||
|
for file_name in file_list:
|
||||||
|
if file_name[0].isdigit():
|
||||||
|
number_list.append(int(file_name.split('-')[0]))
|
||||||
|
# trova il massimo
|
||||||
|
if number_list:
|
||||||
|
max_number = max(number_list)
|
||||||
|
maxn=(max_number + 1)
|
||||||
|
|
||||||
|
if os.path.exists('rubrica.txt'):
|
||||||
|
#SE LA RUBRICA ESISTE
|
||||||
|
with open('rubrica.txt', 'a+') as rubrica:
|
||||||
|
rubrica.seek(0)
|
||||||
|
data = rubrica.read()
|
||||||
|
if restaurant_url not in data:
|
||||||
|
#QUANDO IL RISTORATE NON E' PRESENTE IN RUBRICA
|
||||||
|
saveit = input('Vuoi salvare il ristorante in rubrica? [Y|N] ')
|
||||||
|
#PER DEBUG
|
||||||
|
#saveit = "y"
|
||||||
|
if saveit.upper() in ['YES', 'Y', 'SI', 'S']:
|
||||||
|
os.makedirs("DATI_RUBRICA", exist_ok=True)
|
||||||
|
trova_nuovo_numero()
|
||||||
|
nristmax=(str(maxn) + "-" + nrist + '.html')
|
||||||
|
shutil.move ('JEmenu.html', f'./DATI_RUBRICA/{nristmax}')
|
||||||
|
if data:
|
||||||
|
rubrica.write('\n')
|
||||||
|
rubrica.write(nristmax + '\n')
|
||||||
|
rubrica.write(nrist + '\n')
|
||||||
|
rubrica.write(restaurant_url + '\n')
|
||||||
|
else:
|
||||||
|
#QUANDO IL RISTORATE E' GIA' PRESENTE IN RUBRICA
|
||||||
os.remove("JEmenu.html")
|
os.remove("JEmenu.html")
|
||||||
|
else:
|
||||||
|
#QUANDO IL RISTORATE E' GIA' PRESENTE IN RUBRICA
|
||||||
|
os.remove("JEmenu.html")
|
||||||
|
|
||||||
|
else:
|
||||||
|
#SE LA RUBRICA NON ESISTE
|
||||||
|
with open('rubrica.txt', 'a+') as rubrica:
|
||||||
|
rubrica.seek(0)
|
||||||
|
data = rubrica.read()
|
||||||
|
#CHIEDE SE SI VUOLE SALVARE ALTRIMENTI PULISCE
|
||||||
|
saveit = input('Vuoi salvare il ristorante in rubrica? [Y|N] ')
|
||||||
|
#PER DEBUG
|
||||||
|
#saveit = "y"
|
||||||
|
if saveit.upper() in ['YES', 'Y', 'SI', 'S']:
|
||||||
|
os.makedirs("DATI_RUBRICA", exist_ok=True)
|
||||||
|
nristmax=("1" + "-" + nrist + '.html')
|
||||||
|
shutil.move ('JEmenu.html', f'./DATI_RUBRICA/{nristmax}')
|
||||||
|
if data:
|
||||||
|
rubrica.write('\n')
|
||||||
|
rubrica.write(nristmax + '\n')
|
||||||
|
rubrica.write(nrist + '\n')
|
||||||
|
rubrica.write(restaurant_url + '\n')
|
||||||
|
else:
|
||||||
|
#PULISCE
|
||||||
|
os.remove("JEmenu.html")
|
||||||
|
os.remove("rubrica.txt")
|
||||||
|
|||||||
Reference in New Issue
Block a user