-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_main.py
386 lines (305 loc) · 11.1 KB
/
gui_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import tkinter as tk
import webbrowser
from tkinter import Button, Scrollbar, filedialog, ttk
from tkinter.messagebox import showwarning
from bs4 import BeautifulSoup
import check_html_tags
import process_url
traverse_count = 0
def clear_text_screen():
textBox["state"] = "normal"
textBox.delete(1.0, tk.END)
textBox["state"] = "disabled"
def change_textBox_content(result):
# print(result)
textBox["state"] = "normal"
textBox.insert("1.0", result)
textBox["state"] = "disabled"
# test url = "https://github.com/about"
def set_process_url_input(url):
global result, soup
html_res = process_url.process_url_input(url)
# ignore: the error below on html_res
soup = BeautifulSoup(html_res, "html.parser")
result = soup
change_textBox_content(result)
def formatted_tags(tags):
"""Removes the extra space from tags ex: [" head", " h1 "] => ["head", "h1"]"""
tags = tags.strip()
return tags
def process_tag(tag):
"""Checks if the tag is valid or not. If valid then show it to the textBox"""
tag_list = tag.split(",")
global formatted_tag_list
formatted_tag_list = [formatted_tags(m_tags) for m_tags in tag_list]
for s_tag in formatted_tag_list:
# tag_res : if the tag is valid or not result in boolean
tag_res = check_html_tags.check_tag(s_tag)
if not tag_res:
showwarning(
title="invalid tag {}".format(s_tag), message="please enter a valid tag"
)
return
result = soup.find_all(formatted_tag_list)
print(type(result))
clear_text_screen()
change_textBox_content(result)
def check_for_tags():
if len(formatted_tag_list) == 0:
return False
return True
def process_next_btn():
if not check_for_tags():
showwarning(title="no tags found", message="please enter tags first")
return
global traverse_count, result
print(traverse_count)
clear_text_screen()
temp_res = soup.find_all(formatted_tag_list)
result = temp_res[traverse_count]
change_textBox_content(result)
if traverse_count >= len(temp_res) - 1:
traverse_count = 0
else:
traverse_count += 1
def process_prev_btn():
if not check_for_tags():
showwarning(title="no tags found", message="please enter tags first")
return
global traverse_count, result
print(traverse_count)
clear_text_screen()
temp_res = soup.find_all(formatted_tag_list)
result = temp_res[traverse_count]
change_textBox_content(result)
if traverse_count <= 0:
traverse_count = len(temp_res) - 1
else:
traverse_count -= 1
def OpenUrl(help_url):
"""This will open the help url in the web browser"""
webbrowser.open_new(help_url)
def change_text_wrap_type(wrap_type):
"""This will change the wrap type i.e., char, word, none"""
textBox.configure(wrap=wrap_type)
def create_input_frame(container):
s = ttk.Style()
s.configure("Danger.TFrame", borderwidth=2, relief="sunken")
frame = ttk.Frame(container, style="Danger.TFrame")
# grid column configuration
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
# grid row configuration
frame.grid_rowconfigure(0, weight=2)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_rowconfigure(4, weight=1)
frame.grid_rowconfigure(5, weight=1)
frame.grid_rowconfigure(6, weight=1)
frame.grid_rowconfigure(7, weight=7)
# for getting the info about the width of the column
# ttk.Separator(frame, orient="vertical").grid(
# column=0, row=0, rowspan=17, sticky="nse"
# )
# ttk.Separator(frame, orient="vertical").grid(
# column=1, row=0, rowspan=17, sticky="nse"
# )
# ttk.Separator(frame, orient="vertical").grid(
# column=2, row=0, rowspan=17, sticky="nse"
# )
# label instructing to enter input
ttk.Label(frame, text="Enter input's here", font=("sans", 14)).grid(
column=1, row=0, sticky="w"
)
# label and entry for url
url_label = ttk.Label(frame, text="url")
url_label.grid(column=1, row=1, sticky="w")
# get url input
url = tk.StringVar()
url_entry = ttk.Entry(frame, textvariable=url, width=30)
url_entry.focus()
url_entry.grid(column=1, row=1)
# Submit button to fetch the url result
global submit_button
submit_button = Button(
frame,
width=15,
text="fetch data",
command=lambda: set_process_url_input(url=url_entry.get()),
)
submit_button.grid(column=1, row=2)
# label for instructing multiple inputs
multiple_tags_label = ttk.Label(
frame,
text="you can also use multiple tags separated by comma",
font=("sans", 14),
)
multiple_tags_label.grid(column=1, row=3, columnspan=2, sticky="w")
# label and entry for tag
tags_label = ttk.Label(
frame,
text="tag",
)
tags_label.grid(column=1, row=4, sticky="w")
# entry for tags
tags = tk.StringVar()
tags_entry = ttk.Entry(frame, textvariable=tags, width=30)
tags_entry.grid(column=1, row=4)
# Submit button to get the tag result from the html output
global tag_submit_button
tag_submit_button = Button(
frame,
width=15,
text="get tag result",
command=lambda: process_tag(tags_entry.get()),
)
tag_submit_button.grid(column=1, row=5)
# create a next and previous button to traverse the code and the text
next_button = Button(
frame,
width=15,
text="go to next",
command=process_next_btn,
)
next_button.grid(row=6, column=2, sticky="w")
prev_button = Button(
frame,
width=15,
text="go to prev",
command=process_prev_btn,
)
prev_button.grid(row=6, column=1, sticky="w")
for widget in frame.winfo_children():
widget.grid(padx=0, pady=20)
# create a menubar
menubar = tk.Menu(container)
container.config(menu=menubar)
# create a option menu
option_menu = tk.Menu(container, tearoff=False)
# create help menu
help_menu = tk.Menu(container, tearoff=False)
help_url = "https://github.com/vaibhav135/python-web-scrapping-tool"
help_menu.add_command(label="help", command=lambda: OpenUrl(help_url))
help_menu.add_command(label="quit", command=container.destroy)
# add wrap submenu like wrap = "char"|"word"|"none"
wrap_sub_menu = tk.Menu(option_menu, tearoff=False)
wrap_sub_menu.add_command(
label="char", command=lambda: change_text_wrap_type("char")
)
wrap_sub_menu.add_command(
label="word", command=lambda: change_text_wrap_type("word")
)
wrap_sub_menu.add_command(
label="none", command=lambda: change_text_wrap_type("none")
)
# add a menu item to the menu
option_menu.add_cascade(label="wrap", menu=wrap_sub_menu)
# add the File menu to the menubar
menubar.add_cascade(label="options", menu=option_menu)
menubar.add_cascade(label="help", menu=help_menu)
return frame
def file_save():
f = filedialog.asksaveasfile(mode="w", defaultextension=".txt")
# asksaveasfile return `None` if dialog closed with "cancel".
if f is None:
return
# starts from `1.0`, not `0.0`
text2save = str(result)
f.write(text2save)
# `()` was missing.
f.close()
def remove_empty_lines():
"""remove the extra empty line from the text extracted from the html"""
if raw_text == "":
showwarning(title="no text found", message="please fetch the text first")
return
formatted_lines = raw_text.split("\n")
count = 0
for index, line in enumerate(formatted_lines):
if line == " " or line == "":
count += 1
if count > 2:
formatted_lines[index] = "-"
else:
count = 0
final_line = [line for line in formatted_lines if line != "-"]
new_result = ""
for fl in final_line:
new_result += " " + fl.strip() + "\n"
# print(new_result)
clear_text_screen()
change_textBox_content(new_result)
# extracting text from the html result
def get_text():
global raw_text
raw_text = result.get_text()
clear_text_screen()
change_textBox_content(raw_text)
# reverting the text back to code
def get_code():
result = soup
clear_text_screen()
change_textBox_content(result)
def create_output_frame(container):
s = ttk.Style()
s.configure("Danger.TFrame", borderwidth=2, relief="sunken")
frame = ttk.Frame(container, style="Danger.TFrame")
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
frame.grid_columnconfigure(3, weight=1)
frame.grid_columnconfigure(4, weight=1)
frame.grid_columnconfigure(5, weight=1)
frame.grid_rowconfigure(0, weight=18)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=2)
# making the text global so that when the button is\
# pressed it changes the contents inside of the text widget
global textBox
textBox = tk.Text(frame)
textBox.grid(column=1, row=0, sticky="nsew", columnspan=5)
yscrollbar = Scrollbar(frame, orient="vertical", command=textBox.yview)
xscrollbar = Scrollbar(frame, orient="horizontal", command=textBox.xview)
yscrollbar.grid(column=6, row=0, sticky="ns")
xscrollbar.grid(column=1, row=0, columnspan=6, sticky="sew")
textBox["yscrollcommand"] = yscrollbar.set
textBox["xscrollcommand"] = xscrollbar.set
# do something here
ttk.Separator(frame, orient="horizontal").grid(
column=1, row=1, sticky="ew", columnspan=6
)
# ttk.Label(frame, text="options here soon...").grid(column=1, row=2)
save_button = Button(frame, text="save as", command=lambda: file_save())
save_button.grid(column=1, row=2)
# getting the text from the code
get_text_button = Button(frame, text="get text", command=get_text)
get_text_button.grid(column=2, row=2)
# format the text
format_text = Button(frame, text="format text", command=remove_empty_lines)
format_text.grid(column=3, row=2)
# revert the text state back to code
get_code_button = Button(frame, text="revert back", command=get_code)
get_code_button.grid(column=4, row=2)
# ttk.Label(frame, text="options here soon...").grid(column=1, row=2)
save_button = Button(frame, text="clear textbox", command=clear_text_screen)
save_button.grid(column=5, row=2)
return frame
def create_main_window():
root = tk.Tk()
root.title("Web-Scrapper GUI")
root.geometry("1366x1080+50+50")
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
root.grid_rowconfigure(0, weight=1)
input_frame = create_input_frame(root)
input_frame.grid(column=0, row=0, sticky="nsew", padx=2, pady=2)
output_frame = create_output_frame(root)
output_frame.grid(column=1, row=0, columnspan=2, sticky="nsew", pady=2, padx=2)
root.mainloop()
result = ""
soup = ""
raw_text = ""
formatted_tag_list = []
if __name__ == "__main__":
create_main_window()