-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (44 loc) · 1.75 KB
/
main.py
File metadata and controls
53 lines (44 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from tkinter import *
import requests
from bs4 import BeautifulSoup
root = Tk()
root.title("Web Scraper")
root.geometry("500x300")
def save_webpage_content():
url = entry.get().strip()
if url:
try:
r = requests.get(url)
r.raise_for_status() # Raise exception for invalid response
soup = BeautifulSoup(r.content, "html.parser")
all_text = soup.get_text(separator='\n') # Extract only text from url
save_file_name = entry_1.get().strip()
if save_file_name:
with open(save_file_name, "w", encoding="utf-8") as file:
file.write(all_text)
status_label.config(text=f"File '{save_file_name}' saved successfully!", fg="green")
else:
status_label.config(text="Please provide a valid file name.", fg="red")
except requests.exceptions.RequestException as e:
status_label.config(text=f"Error: {str(e)}", fg="red")
except Exception as e:
status_label.config(text=f"Error: {str(e)}", fg="red")
else:
status_label.config(text="Please provide a valid URL.", fg="red")
# Entry and Label for URL input
url_label = Label(root, text="Enter URL:", font=('Arial', 14))
url_label.pack(pady=10)
entry = Entry(root, width=50)
entry.pack()
# Entry and Label for file name input
file_label = Label(root, text="File Name:", font=('Arial', 14))
file_label.pack(pady=10)
entry_1 = Entry(root, width=50)
entry_1.pack()
# Button to save content
save_button = Button(root, text="SAVE", command=save_webpage_content)
save_button.pack(pady=20)
# Status Label to show save status
status_label = Label(root, text="", font=('Arial', 12))
status_label.pack()
root.mainloop()