amazonas3 / app.py
SiteOn's picture
Update app.py
44128de verified
Raw History Blame Contribute Delete
6.86 kB
import requests
import gradio as gr
from bs4 import BeautifulSoup
#Portal Metrópolis ----------------------------------- Portal Metrópolis ------------------------ Portal Metrópolis
# Função para raspar as notícias do Metrópoles
def raspar_noticias_metropoles():
try:
# Adicionando um timeout de 10 segundos
response = requests.get('https://www.metropoles.com/', 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')
# Matéria principal ---------------- Matéria principal ---------------- Matéria principal
noticias_principal = site.findAll('article', attrs={'class': 'NoticiaWrapper__Article-sc-1vgx9gu-1 bkXtwf'})
resultados = ["<div style='font-size: 20px;'><strong>----------------Portal Metrópoles - Últimas Notícias----------------</strong></div><br>"]
for noticia in noticias_principal:
tprincipal = noticia.find('h2')
titulo = noticia.find('h4')
subtitulo = noticia.find('p', attrs={'class': 'Text__TextBase-sc-1d75gww-0 eCHqr'})
link = noticia.find('a')['href'] if noticia.find('a') else 'Não disponível'
resultados.append(
f"<div><strong>Título:</strong> {titulo.text if titulo else tprincipal.text}</div>"
f"<div><strong>Subtítulo:</strong> {subtitulo.text if subtitulo else 'Não disponível'}</div>"
f"<div><strong>Link:</strong> <a href='{link}' target='_blank'>{link}</a></div><br>"
)
#--------------------------------------------------------------------------------------
# Noticia 2 ---------------- Noticia 2 --------------- Noticia 2
noticias_outras = site.findAll('div', attrs={'class': 'Grid__Col-sc-owmjhw-2 ilHyyP col-submanchete col-submanchete-wide'})
for noticia in noticias_outras:
titulo = noticia.find('h4', attrs={'class': 'Text__TextBase-sc-1d75gww-0 jJCxnn noticia__titulo'})
link = noticia.find('a')['href'] if noticia.find('a') else 'Não disponível'
noticia_relacionada = noticia.find('h3', attrs={'class': 'Text__TextBase-sc-1d75gww-0 jciATu'})
resultados.append(
f"<div><strong>Título:</strong> {titulo.text if titulo else 'Não disponível'}</div>"
f"<div><strong>Link:</strong> <a href='{link}' target='_blank'>{link}</a></div>"
f"<div><strong>Notícia Relacionada:</strong> {noticia_relacionada.text if noticia_relacionada else 'Não disponível'}</div><br>"
)
# Fim Noticia 2 ---------------- Fim Noticia 2 --------------- Fim Noticia 2
#--------------------------------------------------------------------------------------
# Noticia 3 ---------------- Noticia 3 --------------- Noticia 3
noticias3 = site.findAll('h4', attrs={'class': 'Text__TextBase-sc-1d75gww-0 hYzVOu noticia__titulo'})
for noticia in noticias3:
titulo = noticia.find('a') if noticia.find('a') else 'Não disponível'
link = noticia.find('a')['href'] if noticia.find('a') else 'Não disponível'
noticia_relacionada = noticia.find('h4', attrs={'class': 'Text__TextBase-sc-1d75gww-0 edvUXe noticia__titulo'})
resultados.append(
f"<div><strong>Título:</strong> {titulo.text if titulo else 'Não disponível'}</div>"
f"<div><strong>Link:</strong> <a href='{link}' target='_blank'>{link}</a></div>"
f"<div><strong>Notícia Relacionada:</strong> {noticia_relacionada.text if noticia_relacionada else 'Não disponível'}</div><br>"
)
# Fim Noticia 3 ---------------- Fim Noticia 3 --------------- Fim Noticia 3
#--------------------------------------------------------------------------------------
# Noticia 4 ---------------- Noticia 4 --------------- Noticia 4
noticias4 = site.findAll('h3', attrs={'class': 'Text__TextBase-sc-1d75gww-0 IQQVp noticia__titulo'})
for noticia in noticias4:
titulo = noticia.find('a') if noticia.find('a') else 'Não disponível'
link = noticia.find('a')['href'] if noticia.find('a') else 'Não disponível'
noticia_relacionada = noticia.find('h4', attrs={'class': 'Text__TextBase-sc-1d75gww-0 edvUXe noticia__titulo'})
resultados.append(
f"<div><strong>Título:</strong> {titulo.text if titulo else 'Não disponível'}</div>"
f"<div><strong>Link:</strong> <a href='{link}' target='_blank'>{link}</a></div>"
f"<div><strong>Notícia Relacionada:</strong> {noticia_relacionada.text if noticia_relacionada else 'Não disponível'}</div><br>"
)
# Fim Noticia 4 ---------------- Fim Noticia 4 --------------- Fim Noticia 4
#--------------------------------------------------------------------------------------
# Noticia 5 ---------------- Noticia 5 --------------- Noticia 5
noticias5 = site.findAll('h', attrs={'class': 'Text__TextBase-sc-1d75gww-0 IQQVp noticia__titulo'})
for noticia in noticias4:
titulo = noticia.find('a') if noticia.find('a') else 'Não disponível'
link = noticia.find('a')['href'] if noticia.find('a') else 'Não disponível'
noticia_relacionada = noticia.find('h4', attrs={'class': 'Text__TextBase-sc-1d75gww-0 edvUXe noticia__titulo'})
resultados.append(
f"<div><strong>Título:</strong> {titulo.text if titulo else 'Não disponível'}</div>"
f"<div><strong>Link:</strong> <a href='{link}' target='_blank'>{link}</a></div>"
f"<div><strong>Notícia Relacionada:</strong> {noticia_relacionada.text if noticia_relacionada else 'Não disponível'}</div><br>"
)
# Fim Noticia 5 ---------------- Fim Noticia 5 --------------- Fim Noticia 5
#--------------------------------------------------------------------------------------
return "\n".join(resultados)
iface_metropoles = gr.Interface(
fn=raspar_noticias_metropoles,
inputs=[],
outputs="html",
title="Metrópoles - Notícias",
description="Fique por dentro das últimas notícias do Metrópoles",
css="footer.svelte-1rjryqp.svelte-1rjryqp.svelte-1rjryqp {display: none !important;} .panel.svelte-vt1mxs {background: #fff !important;} .show-api {display: none !important;} .svelte-mpyp5e a {display: none !important;} .textarea {font-size: 20px !important; }",
allow_flagging= "never"
)
iface_metropoles.launch(share=True)
#-----------------------------------------------------------