74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
import os, logging, requests, re
|
|
from flask import Flask, render_template
|
|
from bs4 import BeautifulSoup
|
|
|
|
app = Flask(__name__)
|
|
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
|
logger = logging.getLogger("ABC_Scraper")
|
|
|
|
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'}
|
|
|
|
def scrape_abc():
|
|
url = "https://abc-toulouse.fr/horaires.html"
|
|
logger.info(f"\n[DEBUG] Scraping ABC (Sélecteurs précis) sur : {url}")
|
|
|
|
movies_list = []
|
|
try:
|
|
r = requests.get(url, headers=HEADERS, timeout=15)
|
|
soup = BeautifulSoup(r.content, 'html.parser')
|
|
|
|
# 1. On cherche tous les blocs de films
|
|
# D'après ton code source, chaque film commence par cette div
|
|
movie_blocks = soup.find_all('div', class_='table_infos')
|
|
logger.info(f"[DEBUG] {len(movie_blocks)} blocs 'table_infos' trouvés.")
|
|
|
|
for block in movie_blocks:
|
|
# 2. Extraction du Titre (dans le h2)
|
|
title_tag = block.find('h2')
|
|
if not title_tag: continue
|
|
title = title_tag.get_text().strip()
|
|
|
|
# 3. Extraction de l'Affiche (dans col_poster)
|
|
img_tag = block.find('img')
|
|
img_url = ""
|
|
if img_tag:
|
|
img_url = img_tag.get('src')
|
|
if img_url and not img_url.startswith('http'):
|
|
img_url = "https://abc-toulouse.fr" + img_url
|
|
|
|
# 4. Extraction des Horaires
|
|
# Les séances (bulles bleues) sont souvent dans la div suivante
|
|
# ou un frère direct du bloc table_infos.
|
|
# On cherche les horaires (00:00) dans le texte qui suit immédiatement le bloc.
|
|
next_div = block.find_next_sibling()
|
|
showtimes_text = next_div.get_text(" ") if next_div else ""
|
|
|
|
# Si on ne trouve rien dans le frère, on cherche dans le bloc lui-même
|
|
# (au cas où la structure varierait)
|
|
if not re.search(r'\d{2}:\d{2}', showtimes_text):
|
|
showtimes_text = block.get_text(" ")
|
|
|
|
times = re.findall(r'(\d{2}:\d{2})', showtimes_text)
|
|
unique_times = sorted(list(set(times)))
|
|
|
|
if unique_times:
|
|
logger.info(f"✅ TROUVÉ : {title} | {len(unique_times)} séances.")
|
|
movies_list.append({
|
|
"title": title,
|
|
"img": img_url,
|
|
"times": unique_times
|
|
})
|
|
|
|
except Exception as e:
|
|
logger.error(f"❌ Erreur : {e}")
|
|
|
|
return movies_list
|
|
|
|
@app.route('/')
|
|
def index():
|
|
films = scrape_abc()
|
|
return render_template('index.html', films=films)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000)
|