Spaces:
Runtime error
Runtime error
Download scraper.py from FlashCapital/flash-api: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/spaces/FlashCapital/flash-api/resolve/main/scraper.py
- Command line
-
hf download hf://spaces/FlashCapital/flash-api/scraper.py
-
curl -L -o scraper.py https://huggingface.co/spaces/FlashCapital/flash-api/resolve/main/scraper.py
3.38 kB
| import feedparser | |
| import requests | |
| from bs4 import BeautifulSoup | |
| # --- LISTA BLANCA EXPANDIDA: ACCIONES + DEUDA + MACRO --- | |
| LISTA_BLANCA_FINANCIERA = [ | |
| # --- Acciones (Equity) --- | |
| "Mercantil", "MVZ", "Banco de Venezuela", "BDV", "BNC", "Provincial", "BPV", | |
| "CANTV", "TDV", "Ron Santa Teresa", "RST", "FVI", "Envases", "Domínguez", | |
| "Protinal", "BVC", "IBC", "Sivensa", "Manpa", "Corimon", "Bolpriaven", | |
| # --- Bonos y Deuda (Fixed Income) --- | |
| "Bonos", "Deuda Pública", "Papeles Comerciales", "Vebono", "Eurobonos", | |
| "PDVSA", "Default", "Reestructuración", "Cupón", "Renta Fija", | |
| "Títulos de Cobertura", "TCC", "Certificados de Inversión", "Deuda", | |
| # --- Macro y Regulaciones --- | |
| "BCV", "Encaje Legal", "Tipo de Cambio", "Gaceta Oficial", "Sudeban", | |
| "Sunaval", "Sanciones", "OFAC", "Chevron", "Petróleo", "Dólar", "Inflación", | |
| "Venezuela", "Caracas" | |
| ] | |
| def obtener_ultimas_noticias(): | |
| print("🕵️♂️ Buscando noticias en Banca y Negocios...") | |
| url_rss = "https://www.bancaynegocios.com/feed/" | |
| # Parseamos el feed RSS | |
| feed = feedparser.parse(url_rss) | |
| noticias_extraidas = [] | |
| # Procesamos las 20 noticias más recientes | |
| for entrada in feed.entries[:20]: | |
| try: | |
| # --- FILTRO PREVENTIVO (EL "PORTERO") --- | |
| titulo_minuscula = entrada.title.lower() | |
| # Verificamos si alguna palabra clave está en el titular | |
| es_relevante = any(keyword.lower() in titulo_minuscula for keyword in LISTA_BLANCA_FINANCIERA) | |
| if not es_relevante: | |
| # Si el titular no tiene nada que ver con nuestra lista, lo saltamos y ahorramos tiempo | |
| print(f"⏭️ Descartada por el Portero (No relevante): {entrada.title[:50]}...") | |
| continue | |
| # Headers modernos para evitar bloqueos y timeout de 15s por estabilidad | |
| headers = { | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36' | |
| } | |
| # Petición con tiempo de espera extendido para redes inestables | |
| respuesta = requests.get(entrada.link, headers=headers, timeout=15) | |
| soup = BeautifulSoup(respuesta.content, 'html.parser') | |
| # Buscamos todos los párrafos de la noticia | |
| parrafos = soup.find_all('p') | |
| # Filtro de calidad: ignoramos textos cortos (publicidad, avisos, etc.) | |
| texto_limpio = [p.text.strip() for p in parrafos if len(p.text.strip()) > 35] | |
| # Unimos los párrafos más importantes (hasta 8) para dar contexto a la IA | |
| texto_completo = " ".join(texto_limpio[:8]) | |
| if len(texto_completo) > 150: | |
| noticias_extraidas.append({ | |
| "titular_original": entrada.title, | |
| "enlace": entrada.link, | |
| "texto_crudo": texto_completo | |
| }) | |
| print(f"✅ Noticia capturada con éxito: {entrada.title[:50]}...") | |
| except Exception as e: | |
| print(f"❌ Error al extraer noticia individual: {e}") | |
| return noticias_extraidas |