Advertisement
Najeebsk

WEB-SCRAPING-LINKS.py

Jan 8th, 2024
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox, filedialog
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from urllib.parse import urljoin
  6.  
  7. class LinkScraperGUI:
  8.     def __init__(self, master):
  9.         self.master = master
  10.         self.master.title("Najeeb Shah Khan Web Links Scraper and save Text")
  11.  
  12.         self.url_label = tk.Label(master, text="Enter URL:")
  13.         self.url_label.pack()
  14.  
  15.         self.url_entry = tk.Entry(master, width=100)
  16.         self.url_entry.pack()
  17.  
  18.         self.scrape_button = tk.Button(master, text="Scrape Links", command=self.scrape_links)
  19.         self.scrape_button.pack()
  20.  
  21.     def scrape_links(self):
  22.         url = self.url_entry.get()
  23.  
  24.         if not url:
  25.             messagebox.showerror("Error", "Please enter a valid URL.")
  26.             return
  27.  
  28.         try:
  29.             response = requests.get(url)
  30.             response.raise_for_status()
  31.         except requests.exceptions.RequestException as e:
  32.             messagebox.showerror("Error", f"Error fetching URL: {e}")
  33.             return
  34.  
  35.         soup = BeautifulSoup(response.text, 'html.parser')
  36.  
  37.         # Extract all links from the webpage
  38.         links = soup.find_all('a', href=True)
  39.  
  40.         if not links:
  41.             messagebox.showinfo("Info", "No links found on the given URL.")
  42.             return
  43.  
  44.         # Ensure the links are complete URLs
  45.         complete_links = [urljoin(url, link['href']) for link in links]
  46.  
  47.         # Prompt user to choose a file to save the links
  48.         file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
  49.  
  50.         if not file_path:
  51.             return  # User canceled the save operation
  52.  
  53.         with open(file_path, 'w', encoding='utf-8') as file:
  54.             # Write the complete links to the file
  55.             for link in complete_links:
  56.                 file.write(link + '\n')
  57.  
  58.         messagebox.showinfo("Success", f"Complete URLs saved to {file_path}")
  59.  
  60. if __name__ == "__main__":
  61.     root = tk.Tk()
  62.     app = LinkScraperGUI(root)
  63.     root.mainloop()
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement