-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
I think it would be useful to extract as well from main page the number of wins & nominations.
The following script which give 2 methods retrieving the text
-- coding: utf-8 --
"""
Created on Fri Nov 28 06:33:57 2025
@author: gawli
"""
import requests
from bs4 import BeautifulSoup
def extraire_recompenses_imdb(url):
"""
Extrait les informations sur les récompenses et nominations d'une page IMDb.
Teste les deux méthodes et retourne les résultats de chacune.
Args:
url (str): URL de la page IMDb
Returns:
dict: Dictionnaire contenant les résultats des deux méthodes
"""
# Headers pour simuler un navigateur
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
resultats = {
'methode_1': None,
'methode_2': None
}
try:
# Récupération de la page
response = requests.get(url, headers=headers)
response.raise_for_status()
# Parse le HTML
soup = BeautifulSoup(response.content, 'html.parser')
# Méthode 1: Recherche directe par span avec texte contenant "win" ou "nomination"
print("\n=== Test Méthode 1 ===")
awards_span = soup.find('span', class_='ipc-metadata-list-item__list-content-item',
string=lambda text: text and ('win' in text.lower() or 'nomination' in text.lower()))
if awards_span:
resultats['methode_1'] = awards_span.get_text(strip=True)
print(f"✓ Méthode 1 réussie: {resultats['methode_1']}")
else:
print("✗ Méthode 1 échouée")
# Méthode 2: Recherche via la section "Awards"
print("\n=== Test Méthode 2 ===")
# Trouve le lien "Awards"
awards_link = soup.find('a', class_='ipc-metadata-list-item__label', string=lambda text: text and 'Awards' in text)
if awards_link:
print("✓ Section 'Awards' trouvée")
# Remonte au parent pour trouver le conteneur
parent = awards_link.find_parent('li') or awards_link.find_parent('div')
if parent:
# Cherche le span avec les informations
span = parent.find('span', class_='ipc-metadata-list-item__list-content-item')
if span:
text = span.get_text(strip=True)
if 'win' in text.lower() or 'nomination' in text.lower():
resultats['methode_2'] = text
print(f"✓ Méthode 2 réussie: {resultats['methode_2']}")
else:
print(f" → Span trouvé mais ne contient pas de wins/nominations: '{text}'")
else:
print(" → Span non trouvé dans le parent")
else:
print(" → Parent non trouvé")
else:
print("✗ Section 'Awards' non trouvée")
if not resultats['methode_2']:
print("✗ Méthode 2 échouée")
return resultats
except requests.RequestException as e:
print(f"Erreur lors de la récupération de la page: {e}")
return resultats
except Exception as e:
print(f"Erreur lors de l'extraction: {e}")
return resultats
Exemple d'utilisation
if name == "main":
url = "https://www.imdb.com/title/tt0019130/"
resultats = extraire_recompenses_imdb(url)
print("\n" + "="*50)
print("RÉSUMÉ DES RÉSULTATS")
print("="*50)
print(f"Méthode 1: {resultats['methode_1']}")
print(f"Méthode 2: {resultats['methode_2']}")
if resultats['methode_1'] or resultats['methode_2']:
print("\n✓ Au moins une méthode a fonctionné!")
else:
print("\n✗ Aucune méthode n'a trouvé les informations.")
Metadata
Metadata
Assignees
Labels
No labels