-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
196 lines (147 loc) · 7 KB
/
main.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import tkinter as tk
from tkinter import messagebox, scrolledtext, filedialog, ttk
import json
import os
import requests
import threading
import time
from scraper import scrape_website, get_selector_preview
from utils import export_to_csv, export_to_json, export_to_txt, load_config, save_config, configure_headers_and_proxies
# Global variable to store scraping results
scraped_results = []
progress_lock = threading.Lock()
# Function to handle the scrape button click
def scrape():
url = url_entry.get().strip()
title_selector = title_selector_entry.get().strip()
link_selector = link_selector_entry.get().strip()
pages = int(pages_entry.get().strip()) if pages_entry.get().strip() else 1
headers = headers_entry.get().strip()
proxies = proxies_entry.get().strip()
if not url or not title_selector or not link_selector:
messagebox.showerror("Error", "Please enter URL and both selectors.")
return
try:
progress_bar.start()
root.update()
# Set headers and proxies if provided
headers_dict = configure_headers_and_proxies(headers, proxies)
# Create a separate thread for scraping to avoid blocking the main GUI thread
threading.Thread(target=run_scraping, args=(url, title_selector, link_selector, pages, headers_dict)).start()
except Exception as e:
progress_bar.stop()
messagebox.showerror("Error", f"An error occurred: {e}")
# Scraping function that runs in a separate thread
def run_scraping(url, title_selector, link_selector, pages, headers):
try:
global scraped_results
scraped_results = scrape_website(url, title_selector, link_selector, pages, headers)
progress_bar.stop()
display_results(scraped_results)
except Exception as e:
progress_bar.stop()
messagebox.showerror("Error", f"Scraping failed: {e}")
# Function to display the scraped results in the text box
def display_results(results):
results_text.delete(1.0, tk.END) # Clear previous results
if results:
for title, link in results:
results_text.insert(tk.END, f"Title: {title}\nLink: {link}\n\n")
else:
results_text.insert(tk.END, "No results found.\n")
# Function to handle export button click
def export_results():
if not scraped_results:
messagebox.showerror("Error", "No results to export")
return
file_path = filedialog.asksaveasfilename(
defaultextension=".csv",
filetypes=[("CSV files", "*.csv"), ("JSON files", "*.json"), ("Text files", "*.txt"), ("All files", "*.*")]
)
if file_path:
if file_path.endswith(".csv"):
export_to_csv(scraped_results, file_path)
elif file_path.endswith(".json"):
export_to_json(scraped_results, file_path)
elif file_path.endswith(".txt"):
export_to_txt(scraped_results, file_path)
messagebox.showinfo("Success", f"Results exported to {file_path}")
# Load saved configurations
def load_saved_config():
config = load_config()
if config:
url_entry.insert(0, config["url"])
title_selector_entry.insert(0, config["title_selector"])
link_selector_entry.insert(0, config["link_selector"])
pages_entry.insert(0, str(config["pages"]))
headers_entry.insert(0, config["headers"])
proxies_entry.insert(0, config["proxies"])
# Function to preview selectors (live testing)
def test_selectors():
url = url_entry.get().strip()
title_selector = title_selector_entry.get().strip()
link_selector = link_selector_entry.get().strip()
if not url or not title_selector or not link_selector:
messagebox.showerror("Error", "Please enter URL and selectors.")
return
preview = get_selector_preview(url, title_selector, link_selector)
selector_preview_text.delete(1.0, tk.END)
selector_preview_text.insert(tk.END, preview)
# Create the GUI application window
root = tk.Tk()
root.title("Advanced Web Scraper")
root.geometry("900x700")
root.configure(bg="#f5f5f5")
# Set the color scheme
PRIMARY_COLOR = "#4CAF50"
SECONDARY_COLOR = "#2196F3"
BUTTON_COLOR = "#FFC107"
TEXT_COLOR = "#333333"
# Create and style the widgets
url_label = tk.Label(root, text="Enter URL:", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
url_label.pack(pady=10)
url_entry = tk.Entry(root, width=60, font=("Arial", 12), relief="solid", borderwidth=2)
url_entry.pack(pady=5)
title_selector_label = tk.Label(root, text="Enter Title Selector:", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
title_selector_label.pack(pady=10)
title_selector_entry = tk.Entry(root, width=60, font=("Arial", 12), relief="solid", borderwidth=2)
title_selector_entry.pack(pady=5)
link_selector_label = tk.Label(root, text="Enter Link Selector:", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
link_selector_label.pack(pady=10)
link_selector_entry = tk.Entry(root, width=60, font=("Arial", 12), relief="solid", borderwidth=2)
link_selector_entry.pack(pady=5)
pages_label = tk.Label(root, text="Number of Pages to Scrape:", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
pages_label.pack(pady=10)
pages_entry = tk.Entry(root, width=60, font=("Arial", 12), relief="solid", borderwidth=2)
pages_entry.pack(pady=5)
headers_label = tk.Label(root, text="Custom Headers (Optional):", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
headers_label.pack(pady=10)
headers_entry = tk.Entry(root, width=60, font=("Arial", 12), relief="solid", borderwidth=2)
headers_entry.pack(pady=5)
proxies_label = tk.Label(root, text="Custom Proxies (Optional):", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
proxies_label.pack(pady=10)
proxies_entry = tk.Entry(root, width=60, font=("Arial", 12), relief="solid", borderwidth=2)
proxies_entry.pack(pady=5)
scrape_button = tk.Button(root, text="Scrape", command=scrape, bg=PRIMARY_COLOR, fg="white", font=("Arial", 12),
relief="solid")
scrape_button.pack(pady=15)
test_button = tk.Button(root, text="Test Selectors", command=test_selectors, bg=BUTTON_COLOR, fg="white",
font=("Arial", 12), relief="solid")
test_button.pack(pady=10)
export_button = tk.Button(root, text="Export Results", command=export_results, bg=BUTTON_COLOR, fg="white",
font=("Arial", 12), relief="solid")
export_button.pack(pady=10)
progress_bar = ttk.Progressbar(root, mode="indeterminate", length=300)
progress_bar.pack(pady=15)
results_label = tk.Label(root, text="Results:", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
results_label.pack(pady=10)
results_text = scrolledtext.ScrolledText(root, width=70, height=15, font=("Arial", 10), wrap=tk.WORD)
results_text.pack(pady=5)
selector_preview_label = tk.Label(root, text="Selector Preview:", bg="#f5f5f5", fg=TEXT_COLOR, font=("Arial", 12))
selector_preview_label.pack(pady=10)
selector_preview_text = scrolledtext.ScrolledText(root, width=70, height=10, font=("Arial", 10), wrap=tk.WORD)
selector_preview_text.pack(pady=5)
# Load saved configurations on start
load_saved_config()
# Run the application
root.mainloop()