Advertisement
MizunoBrasil

Raspagem Globo (Janela)

Feb 14th, 2024
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import tkinter as tk
  2. import webbrowser
  3. import requests
  4. from bs4 import BeautifulSoup
  5. import re
  6. from datetime import datetime
  7.  
  8. def extrair_informacao(url):
  9.     # Faz uma solicitação HTTP para a URL fornecida
  10.     response = requests.get(url)
  11.    
  12.     # Verifica se a solicitação foi bem-sucedida
  13.     if response.status_code == 200:
  14.         # Cria um objeto BeautifulSoup para fazer o parsing do conteúdo HTML
  15.         soup = BeautifulSoup(response.content, 'html.parser')
  16.        
  17.         # Extrai o título da página
  18.         title = soup.title.string
  19.        
  20.         # Extrai a primeira frase de texto da página
  21.         first_paragraph = soup.find('p').text.strip()
  22.        
  23.         # Extrai o texto da seção content-head__subtitle
  24.         subtitle = soup.find(class_='content-head__subtitle').text.strip()
  25.        
  26.         # Retorna as informações extraídas
  27.         return title, first_paragraph, subtitle
  28.     else:
  29.         print("Falha ao fazer a solicitação HTTP.")
  30.         return None, None, None
  31.  
  32. def abrir_url():
  33.     url = url_entry.get()
  34.     webbrowser.open_new(url)
  35.  
  36. def extrair_e_mostrar_informacoes():
  37.     url = url_entry.get()
  38.     title, first_paragraph, subtitle = extrair_informacao(url)
  39.    
  40.     if title and first_paragraph and subtitle:
  41.         info_text.config(text=f"Título da página: {title}\n\n"
  42.                                f"Primeira frase de texto: {first_paragraph}\n\n"
  43.                                f"Texto da seção content-head__subtitle: {subtitle}")
  44.     else:
  45.         info_text.config(text="Não foi possível extrair informações da página.")
  46.  
  47. # Criar a janela
  48. root = tk.Tk()
  49. root.title("Extrair Informações da Página")
  50. root.geometry("600x400")
  51.  
  52. # Criar um rótulo para o campo de URL
  53. url_label = tk.Label(root, text="URL:")
  54. url_label.pack(pady=10)
  55.  
  56. # Criar um campo de entrada para a URL
  57. url_entry = tk.Entry(root, width=50)
  58. url_entry.pack()
  59.  
  60. # Criar um botão para abrir a URL no navegador
  61. open_button = tk.Button(root, text="Abrir URL", command=abrir_url)
  62. open_button.pack(pady=5)
  63.  
  64. # Criar um botão para extrair e mostrar informações
  65. extract_button = tk.Button(root, text="Extrair e Mostrar Informações", command=extrair_e_mostrar_informacoes)
  66. extract_button.pack(pady=5)
  67.  
  68. # Criar um rótulo para mostrar as informações extraídas
  69. info_text = tk.Label(root, text="", wraplength=580, justify="left")
  70. info_text.pack(pady=10)
  71.  
  72. # Executar a janela
  73. root.mainloop()
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement