Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +54 -0
- requeriments.txt +3 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import gradio as gr
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
|
5 |
+
def scrape_blogbraga():
|
6 |
+
base_url = 'https://blogbraga.com.br' # Definindo a variável base_url dentro da função
|
7 |
+
|
8 |
+
try:
|
9 |
+
# Adicionando um timeout de 10 segundos
|
10 |
+
response = requests.get(base_url, timeout=10)
|
11 |
+
except requests.Timeout:
|
12 |
+
return "O pedido excedeu o tempo limite. Tente novamente mais tarde."
|
13 |
+
except requests.RequestException as e:
|
14 |
+
return f"Erro ao fazer a requisição: {e}"
|
15 |
+
|
16 |
+
content = response.content
|
17 |
+
site = BeautifulSoup(content, 'html.parser')
|
18 |
+
|
19 |
+
noticias = site.findAll('div', attrs={'class': 'news'})
|
20 |
+
resultados = ["<div style='font-size: 20px;'><strong>----------------Blog Braga - Últimas Notícias----------------</strong></div><br>"]
|
21 |
+
|
22 |
+
for noticia in noticias:
|
23 |
+
categoria_materia = noticia.find('h2', attrs={'class': 'category'})
|
24 |
+
categoria_text = categoria_materia.text.strip() if categoria_materia else "Sem categoria"
|
25 |
+
|
26 |
+
H_postagem = noticia.find('h2', attrs={'class': 'timer'})
|
27 |
+
H_postagem_text = H_postagem.text.strip() if H_postagem else "Sem horário de postagem"
|
28 |
+
|
29 |
+
titulo_element = noticia.find('a')
|
30 |
+
titulo_text = titulo_element.text.strip() if titulo_element else "Sem título"
|
31 |
+
|
32 |
+
link = titulo_element['href'] if titulo_element else "#"
|
33 |
+
full_url = base_url + link if titulo_element else "#"
|
34 |
+
|
35 |
+
resultados.append(
|
36 |
+
f"<div><strong>Categoria:</strong> {categoria_text}</div>"
|
37 |
+
f"<div><strong>Horário de Postagem:</strong> {H_postagem_text}</div>"
|
38 |
+
f"<div><strong>Título:</strong> {titulo_text}</div>"
|
39 |
+
f"<div><strong>Link:</strong> <a href='{full_url}' target='_blank'>{full_url}</a></div><br>"
|
40 |
+
)
|
41 |
+
|
42 |
+
return "\n".join(resultados)
|
43 |
+
|
44 |
+
iface_blogbraga = gr.Interface(
|
45 |
+
fn=scrape_blogbraga,
|
46 |
+
inputs=[],
|
47 |
+
outputs="html",
|
48 |
+
title="Blog Braga - Notícias",
|
49 |
+
description="Fique por dentro das últimas notícias do Blog Braga",
|
50 |
+
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; }",
|
51 |
+
allow_flagging= "never"
|
52 |
+
)
|
53 |
+
|
54 |
+
iface_blogbraga.launch(share=True)
|
requeriments.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
gradio
|
3 |
+
beautifulsoup4
|