Skip to content

Commit d41e8f1

Browse files
committed
number of questions options added to GUI
1 parent b89eb9c commit d41e8f1

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Stack-overflow-scraper/scraper.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,21 @@ def get_URL():
4343
url = 'https://stackoverflow.com/questions/tagged/{}?sort=MostVotes&edited=true'.format(tag)
4444
return url
4545

46+
def number_questions():
47+
questions = int(questions_box.get())
48+
if type(questions) != int or questions > 15:
49+
return 15
50+
return questions
51+
4652
def scrape_questions():
4753
for count in range(5):
4854
progress['value'] += 15
4955
window.update_idletasks()
5056
time.sleep(0.10)
5157

58+
question_count = number_questions()
59+
count = 0
60+
5261
url = get_URL()
5362
if url:
5463
page = requests.get(url)
@@ -64,6 +73,8 @@ def scrape_questions():
6473
clear_progress()
6574
return ""
6675
for question in questions:
76+
if count >= question_count:
77+
break
6778
question_text = question.find('a', {'class': 'question-hyperlink'}).text.strip()
6879
question_summary = question.find('div', {'class': 'excerpt'}).text.strip()
6980
question_summary = question_summary.replace('\n',' ')
@@ -72,6 +83,7 @@ def scrape_questions():
7283
views = question.find('div', {'class': 'views'}).text.strip().split()[0]
7384
entities = (question_text, question_summary, question_link, votes, views)
7485
sql_insert(con, entities)
86+
count += 1
7587

7688
messagebox.showinfo("Success!", "Questions scrapped successfully!")
7789
clear_progress()
@@ -143,23 +155,30 @@ def clear_progress():
143155
background='white', foreground="Orange",
144156
font=("Helvetica", 30, 'bold')).grid(row=0, column=1)
145157

146-
# label for combobox
158+
# label texts
147159
ttk.Label(window, text="Enter tag (ex - python):", background = 'white',
148160
font=("Helvetica", 15)).grid(column=0,
149161
row=5, padx=10, pady=25)
150162

163+
ttk.Label(window, text="No of questions to scrape:", background = 'white',
164+
font=("Helvetica", 15)).grid(column=0,
165+
row=6, padx=10, pady=5)
166+
151167

152168
# Button creation
153169
scrape_btn = ttk.Button(window, text="Scrape questions!", style='my.TButton', command=scrape_questions)
154170
scrape_btn.grid(row=5, column=2, pady=5, padx=15, ipadx=5)
155171

156172
display_btn = ttk.Button(window, text="Display from DB", style='my.TButton', command = show_results)
157-
display_btn.grid(row=7, column=2, pady=5, padx=15, ipadx=5)
173+
display_btn.grid(row=6, column=2, pady=5, padx=15, ipadx=5)
158174

159175
# Search Box
160176
search_box = tk.Entry(window, font=("Helvetica 15"), bd = 2, width=60)
161177
search_box.grid(row=5, column=1, pady=5, padx=15, ipadx=5)
162178

179+
questions_box = tk.Entry(window, font=("Helvetica 15"), bd = 2, width=60)
180+
questions_box.grid(row=6, column=1, pady=5, padx=15, ipadx=5)
181+
163182
frame = ttk.Frame(window, style='my.TFrame')
164183
frame.place(relx=0.50, rely=0.18, relwidth=0.98, relheight=0.90, anchor="n")
165184

0 commit comments

Comments
 (0)