Spaces:
Running
Running
import requests | |
import gradio as gr | |
from bs4 import BeautifulSoup | |
def scrape_blogbraga(): | |
base_url = 'https://blogbraga.com.br' # Definindo a variável base_url dentro da função | |
try: | |
# Adicionando um timeout de 10 segundos | |
response = requests.get(base_url, timeout=10) | |
except requests.Timeout: | |
return "O pedido excedeu o tempo limite. Tente novamente mais tarde." | |
except requests.RequestException as e: | |
return f"Erro ao fazer a requisição: {e}" | |
content = response.content | |
site = BeautifulSoup(content, 'html.parser') | |
noticias = site.findAll('div', attrs={'class': 'news'}) | |
resultados = ["<div style='font-size: 20px;'><strong>----------------Blog Braga - Últimas Notícias----------------</strong></div><br>"] | |
for noticia in noticias: | |
categoria_materia = noticia.find('h2', attrs={'class': 'category'}) | |
categoria_text = categoria_materia.text.strip() if categoria_materia else "Sem categoria" | |
H_postagem = noticia.find('h2', attrs={'class': 'timer'}) | |
H_postagem_text = H_postagem.text.strip() if H_postagem else "Sem horário de postagem" | |
titulo_element = noticia.find('a') | |
titulo_text = titulo_element.text.strip() if titulo_element else "Sem título" | |
link = titulo_element['href'] if titulo_element else "#" | |
full_url = base_url + link if titulo_element else "#" | |
resultados.append( | |
f"<div><strong>Categoria:</strong> {categoria_text}</div>" | |
f"<div><strong>Horário de Postagem:</strong> {H_postagem_text}</div>" | |
f"<div><strong>Título:</strong> {titulo_text}</div>" | |
f"<div><strong>Link:</strong> <a href='{full_url}' target='_blank'>{full_url}</a></div><br>" | |
) | |
return "\n".join(resultados) | |
iface_blogbraga = gr.Interface( | |
fn=scrape_blogbraga, | |
inputs=[], | |
outputs="html", | |
title="Blog Braga - Notícias", | |
description="Fique por dentro das últimas notícias do Blog Braga", | |
css="footer.svelte-1rjryqp.svelte-1rjryqp.svelte-1rjryqp {display: none !important;} .panel.svelte-vt1mxs {background: #fff !important;} .show-api {display: none !important;} .built-with.svelte-1rjryqp.svelte-1rjryqp.svelte-1rjryqp {display: none !important;} .svelte-mpyp5e a {display: none !important;} .textarea {font-size: 20px !important; }", | |
allow_flagging= "never" | |
) | |
iface_blogbraga.launch(share=True) |