Advertisement
Najeebsk

ALL-EXT-INDEX-SCRAPER.py

Jan 8th, 2024
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from urllib.parse import urljoin
  6.  
  7. class IndexOfScraperApp:
  8.     def __init__(self, root):
  9.         self.root = root
  10.         self.root.title("Najeeb Shah Khan All Index Of URL Scraper")
  11.  
  12.         self.url_label = ttk.Label(root, text="Enter Index Of URL:")
  13.         self.url_entry = ttk.Entry(root, width=50)
  14.         self.scrape_button = ttk.Button(root, text="Scrape URLs", command=self.scrape_urls)
  15.         self.result_text = tk.Text(root, height=36, width=124, wrap="none", state=tk.DISABLED)
  16.        
  17.         self.url_label.grid(row=0, column=0, pady=5)
  18.         self.url_entry.grid(row=0, column=1, pady=5)
  19.         self.scrape_button.grid(row=0, column=2, pady=5)
  20.         self.result_text.grid(row=1, column=0, columnspan=3, pady=10)
  21.        
  22.     def scrape_urls(self):
  23.         url = self.url_entry.get()
  24.         if not url:
  25.             return
  26.  
  27.         try:
  28.             response = requests.get(url)
  29.             response.raise_for_status()
  30.         except requests.exceptions.RequestException as e:
  31.             self.display_result(f"Error: {e}")
  32.             return
  33.  
  34.         soup = BeautifulSoup(response.content, 'html.parser')
  35.         links = [a['href'] for a in soup.find_all('a', href=True)]
  36.  
  37.         if links:
  38.             absolute_links = [urljoin(url, link) for link in links]
  39.             result_text = "\n".join(absolute_links)
  40.             self.display_result(result_text)
  41.         else:
  42.             self.display_result("No links found in the directory.")
  43.  
  44.     def display_result(self, text):
  45.         self.result_text.config(state=tk.NORMAL)
  46.         self.result_text.delete(1.0, tk.END)
  47.         self.result_text.insert(tk.END, text)
  48.         self.result_text.config(state=tk.DISABLED)
  49.  
  50. if __name__ == "__main__":
  51.     root = tk.Tk()
  52.     app = IndexOfScraperApp(root)
  53.     root.mainloop()
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement