Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox, filedialog
- import requests
- from bs4 import BeautifulSoup
- class WebScraperGUI:
- def __init__(self, master):
- self.master = master
- self.master.title("Najeeb Shah Khan HTML Code Web Scraper")
- self.url_label = tk.Label(master, text="Enter URL:")
- self.url_label.pack()
- self.url_entry = tk.Entry(master, width=120)
- self.url_entry.pack()
- self.scrape_button = tk.Button(master, text="Scrape Web Page", command=self.scrape_web_page)
- self.scrape_button.pack()
- def scrape_web_page(self):
- url = self.url_entry.get()
- if not url:
- messagebox.showerror("Error", "Please enter a valid URL.")
- return
- try:
- response = requests.get(url)
- response.raise_for_status()
- except requests.exceptions.RequestException as e:
- messagebox.showerror("Error", f"Error fetching URL: {e}")
- return
- content = response.text
- # Prompt user to choose a file to save the content
- file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
- if not file_path:
- return # User canceled the save operation
- with open(file_path, 'w', encoding='utf-8') as file:
- # Write the complete content to the file
- file.write(content)
- messagebox.showinfo("Success", f"Web page content saved to {file_path}")
- if __name__ == "__main__":
- root = tk.Tk()
- app = WebScraperGUI(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement