Advertisement
Najeebsk

WEB-SCRAP-ALL-HTML-CODE.py

Jan 8th, 2024
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox, filedialog
  3. import requests
  4. from bs4 import BeautifulSoup
  5.  
  6. class WebScraperGUI:
  7.     def __init__(self, master):
  8.         self.master = master
  9.         self.master.title("Najeeb Shah Khan HTML Code Web Scraper")
  10.  
  11.         self.url_label = tk.Label(master, text="Enter URL:")
  12.         self.url_label.pack()
  13.  
  14.         self.url_entry = tk.Entry(master, width=120)
  15.         self.url_entry.pack()
  16.  
  17.         self.scrape_button = tk.Button(master, text="Scrape Web Page", command=self.scrape_web_page)
  18.         self.scrape_button.pack()
  19.  
  20.     def scrape_web_page(self):
  21.         url = self.url_entry.get()
  22.  
  23.         if not url:
  24.             messagebox.showerror("Error", "Please enter a valid URL.")
  25.             return
  26.  
  27.         try:
  28.             response = requests.get(url)
  29.             response.raise_for_status()
  30.         except requests.exceptions.RequestException as e:
  31.             messagebox.showerror("Error", f"Error fetching URL: {e}")
  32.             return
  33.  
  34.         content = response.text
  35.  
  36.         # Prompt user to choose a file to save the content
  37.         file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
  38.  
  39.         if not file_path:
  40.             return  # User canceled the save operation
  41.  
  42.         with open(file_path, 'w', encoding='utf-8') as file:
  43.             # Write the complete content to the file
  44.             file.write(content)
  45.  
  46.         messagebox.showinfo("Success", f"Web page content saved to {file_path}")
  47.  
  48. if __name__ == "__main__":
  49.     root = tk.Tk()
  50.     app = WebScraperGUI(root)
  51.     root.mainloop()
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement