Advertisement
MizunoBrasil

Monitora urls

Jul 3rd, 2023
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import threading
  4. import time
  5. import webbrowser
  6. from selenium import webdriver
  7.  
  8. class URLMonitor(threading.Thread):
  9.     def __init__(self, treeview, driver):
  10.         threading.Thread.__init__(self)
  11.         self.treeview = treeview
  12.         self.driver = driver
  13.         self.running = True
  14.         self.last_url = ""
  15.  
  16.     def run(self):
  17.         while self.running:
  18.             try:
  19.                 # Obtenha a URL atual
  20.                 url = self.driver.current_url
  21.                 # Obtenha o título da página atual
  22.                 page_title = self.driver.title
  23.  
  24.                 if url != self.last_url:
  25.                     # Adicione a URL e o título à treeview
  26.                     self.treeview.insert("", "end", values=(url, page_title))
  27.                     self.last_url = url
  28.             except Exception as e:
  29.                 print(f"Erro ao capturar URL: {e}")
  30.  
  31.             time.sleep(1)
  32.  
  33.     def stop(self):
  34.         self.running = False
  35.  
  36.  
  37. def save_titles(treeview):
  38.     try:
  39.         with open("titles.html", "w") as file:
  40.             file.write("<html>\n")
  41.             file.write("<body>\n")
  42.  
  43.             items = treeview.get_children()
  44.             for item in items:
  45.                 values = treeview.item(item)["values"]
  46.                 if "URL Monitor" not in values[1] and "Nova guia - Google Chrome" not in values[1]:
  47.                     title = values[1]
  48.                     url = values[0]
  49.                     file.write(f'<a href="{url}">{title}</a><br>\n')
  50.  
  51.             file.write("</body>\n")
  52.             file.write("</html>\n")
  53.         print("Títulos exportados com sucesso.")
  54.     except Exception as e:
  55.         print(f"Erro ao exportar títulos: {e}")
  56.  
  57.  
  58.  
  59.  
  60. def open_browser(event, treeview, driver):
  61.     item = treeview.selection()[0]
  62.     url = treeview.item(item)["values"][0]
  63.     webbrowser.open(url)
  64.  
  65.  
  66. def main():
  67.     # Configurar janela
  68.     root = tk.Tk()
  69.     root.title("URL Monitor")
  70.     root.geometry("500x400")
  71.  
  72.     # Configurar Treeview
  73.     treeview = ttk.Treeview(root)
  74.     treeview["columns"] = ("URL", "Page Title")
  75.     treeview.heading("URL", text="URL")
  76.     treeview.heading("Page Title", text="Page Title")
  77.     treeview.pack(expand=True, fill=tk.BOTH)
  78.  
  79.     # Configurar Selenium WebDriver
  80.     driver = webdriver.Chrome()  # Adapte para o driver que você estiver usando
  81.  
  82.     # Configurar URL Monitor
  83.     monitor = URLMonitor(treeview, driver)
  84.     monitor.start()
  85.  
  86.     def on_closing():
  87.         monitor.stop()
  88.         driver.quit()
  89.         root.destroy()
  90.  
  91.     def export_titles():
  92.         save_titles(treeview)
  93.  
  94.     export_button = ttk.Button(root, text="Exportar", command=export_titles)
  95.     export_button.pack()
  96.  
  97.     treeview.bind("<Double-1>", lambda event: open_browser(event, treeview, driver))
  98.  
  99.     root.protocol("WM_DELETE_WINDOW", on_closing)
  100.     root.mainloop()
  101.  
  102.  
  103. if __name__ == "__main__":
  104.     main()
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement