-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_scrapper.py
388 lines (322 loc) · 18.7 KB
/
web_scrapper.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
387
388
from tkinter import *
from PIL import Image,ImageTk
from bs4 import BeautifulSoup
from tkHyperlinkManager import HyperlinkManager
from Data_set_analysis import *
from functools import partial
import requests
from csv import writer
import webbrowser
import os
url = "https://timesofindia.indiatimes.com/"
news_dataset = []
class web_scrapper:
def __init__(self,window):
self.window = window
self.window.title("Web Scrapper - Times of India!")
self.window.geometry('1200x675+0+0')
# function that searches google
def search(*arg):
headers = {'User-agent': 'Chrome/103.0.5060.134'}
request = requests.get('https://timesofindia.indiatimes.com/', headers=headers)
html = request.content
soup = BeautifulSoup(html, 'html.parser')
if(not arg):
x = e.get()
else:
x = str(arg)[2:-3]
print(x)
def toi_news_scrapper(keyword):
news_list = []
with open('news_scraping.csv', 'w', encoding='utf8', newline='') as f:
thewriter = writer(f)
header = ['Index', 'Head Lines']
thewriter.writerow(header)
v = soup.find_all('figure')
for h in v:
news_title = h.a.figcaption.get_text()
news_link = h.a.get('href')
if news_title not in news_list:
if 'Times of India' not in news_title:
k = [news_title,news_link]
news_list.append(k)
no_of_news = 0
keyword_list = []
# Goes through the list and searches for the keyword
for i in range(1,len(news_list)):
text = ''
if ((keyword in news_list[i][0]) or (keyword in news_list[i][1])):
text = ' <---------- KEYWORD FOUND!'
no_of_news += 1
keyword_list.append(news_list[i])
# Prints the Titles of the articles that contain the keywords
for j in range(len(keyword_list)):
info = [keyword_list[j][0],keyword_list[j][1], text]
thewriter.writerow(info)
# tkinter display
section = Toplevel()
section.title(f"Searched keyword - {keyword}")
section.geometry('1200x675+0+0')
img=Image.open('images/keyword.jpg')
img=img.resize((1200,675),Image.Resampling.LANCZOS)
self.photobg=ImageTk.PhotoImage(img)
t = Label(section, image=self.photobg)
t.place(x=0,y=0)
w_frame = LabelFrame(section,bd=20,bg="white",relief=FLAT,text =f'News on {keyword} -- {no_of_news} Articles :\n' ,font=('calibri',20))
w_frame.place(x=120,y=80,width=900,height=600)
text_b = Text(w_frame,width=800, height=595, font=('calibri',14),blockcursor=True,relief=FLAT)
text_b.pack()
hyperlink = HyperlinkManager(text_b)
for p in range(0,len(keyword_list)):
text_b.insert(INSERT, keyword_list[p][0])
text_b.insert(INSERT,"\n")
text_b.insert(INSERT,"Click to read the article..", hyperlink.add(partial(webbrowser.open,keyword_list[p][1])))
text_b.insert(INSERT,"\n")
text_b.insert(INSERT,"\n")
with open('Searched_news.csv', 'a',encoding='utf8', newline='') as f:
thewriter = writer(f)
header1 = ['Keyword', 'Number of articles found']
file_is_empty = os.stat('Searched_news.csv').st_size==0
if file_is_empty:
thewriter.writerow(header1)
o = [keyword,no_of_news]
news_dataset.clear()
news_dataset.append(o)
thewriter.writerows(news_dataset)
if(x != "Search here.."):
toi_news_scrapper(x)
Boardframe = LabelFrame(self.window,bd=20,bg="black",relief=FLAT,font=('calibri',12))
Boardframe.place(x=120,y=80,width=900,height=200)
# searchbar
e=Entry(Boardframe,width=76,relief=FLAT,borderwidth=8, font=1)
e.insert(0,"Search here..")
e.grid(row=0,column=1)
# popular searches
def popular_searches(name):
if(name == 'b'):
section = Toplevel()
section.title('Business section')
section.geometry('1200x675+0+0')
img=Image.open('images/business.jpg')
img=img.resize((1200,675),Image.Resampling.LANCZOS)
self.photobg=ImageTk.PhotoImage(img)
b = Label(section, image=self.photobg)
b.place(x=0,y=0)
urlb = "https://timesofindia.indiatimes.com/business"
content_requestb = requests.get(urlb)
html_contentb = content_requestb.content
soup_b = BeautifulSoup(html_contentb, 'html.parser')
w_frame = LabelFrame(section,bd=20,bg="white",relief=FLAT,text ="Bussiness news --------\n" ,font=('calibri',20))
w_frame.place(x=120,y=80,width=900,height=600)
text_b = Text(w_frame,width=800, height=595, font=('calibri',14),blockcursor=True,relief=FLAT)
text_b.pack()
all_b = list()
hyperlink = HyperlinkManager(text_b)
figure_b = soup_b.find_all('figure')
for ha in (figure_b):
all_b.append(ha.figcaption.get_text())
all_b.append(ha.a.get('href'))
for p in range(0,len(all_b),2):
text_b.insert(INSERT, all_b[p])
text_b.insert(INSERT,"\n")
text_b.insert(INSERT,"Click to read the article..", hyperlink.add(partial(webbrowser.open,all_b[p+1])))
text_b.insert(INSERT,"\n")
text_b.insert(INSERT,"\n")
if(name == 't'):
section = Toplevel()
section.title('Technology section')
section.geometry('1200x675+0+0')
img=Image.open('images/tech.jpg')
img=img.resize((1200,675),Image.Resampling.LANCZOS)
self.photobg=ImageTk.PhotoImage(img)
t = Label(section, image=self.photobg)
t.place(x=0,y=0)
urlt = "https://www.gadgetsnow.com/"
content_requestt = requests.get(urlt)
html_contentt = content_requestt.content
soup_t = BeautifulSoup(html_contentt, 'html.parser')
w_frame = LabelFrame(section,bd=20,bg="white",relief=FLAT,text ="Tech and Gadgets news --------\n" ,font=('calibri',20))
w_frame.place(x=120,y=80,width=900,height=600)
text_t = Text(w_frame,width=800, height=595, font=('calibri',14),blockcursor=True,relief=FLAT)
text_t.pack()
all_t = list()
hyperlink = HyperlinkManager(text_t)
figure_t = soup_t.find_all('a',class_="")
for ha in (figure_t):
if(ha.figcaption != None):
all_t.append(ha.figcaption.get_text())
all_t.append(ha.get('href'))
for p in range(0,len(all_t),2):
text_t.insert(INSERT, all_t[p])
text_t.insert(INSERT,"\n")
text_t.insert(INSERT,"Click to read the article..", hyperlink.add(partial(webbrowser.open,all_t[p+1])))
text_t.insert(INSERT,"\n")
text_t.insert(INSERT,"\n")
if(name == 's'):
section = Toplevel()
section.title('Sports section')
section.geometry('1200x675+0+0')
img = Image.open('images/sports.jpg')
img = img.resize((1200, 675), Image.Resampling.LANCZOS)
self.photobg = ImageTk.PhotoImage(img)
s = Label(section, image=self.photobg)
s.place(x=0, y=0)
urls = "https://timesofindia.indiatimes.com/sports"
content_requests = requests.get(urls)
html_contents = content_requests.content
soup_s = BeautifulSoup(html_contents, 'html.parser')
w_frame = LabelFrame(section, bd=20, bg="white", relief=FLAT, text="Sports news --------\n",
font=('calibri', 20))
w_frame.place(x=120, y=80, width=900, height=600)
text_s = Text(w_frame, width=800, height=595, font=('calibri', 14), blockcursor=True, relief=FLAT)
text_s.pack()
all_s = list()
hyperlink = HyperlinkManager(text_s)
figure_s = soup_s.find_all('span',class_="w_tle")
for ha in (figure_s):
all_s.append(ha.a.get_text())
all_s.append(ha.a.get('href'))
for p in range(0, len(all_s), 2):
text_s.insert(INSERT, all_s[p])
text_s.insert(INSERT, "\n")
text_s.insert(INSERT, "Click to read the article..",
hyperlink.add(partial(webbrowser.open, all_s[p + 1])))
text_s.insert(INSERT, "\n")
text_s.insert(INSERT, "\n")
if(name == 'n'):
section = Toplevel()
section.title('News section')
section.geometry('1200x675+0+0')
img = Image.open('images/news.jpg')
img = img.resize((1200, 675), Image.Resampling.LANCZOS)
self.photobg = ImageTk.PhotoImage(img)
n = Label(section, image=self.photobg)
n.place(x=0, y=0)
url_n = "https://timesofindia.indiatimes.com/home/headlines"
content_request_n = requests.get(url_n)
html_content_n = content_request_n.content
soup_n = BeautifulSoup(html_content_n, 'html.parser')
w_frame = LabelFrame(section, bd=20, bg="white", relief=FLAT, text="Explainers News --------\n",
font=('calibri', 20))
w_frame.place(x=120, y=80, width=900, height=600)
text_n = Text(w_frame, width=800, height=595, font=('calibri', 14), blockcursor=True, relief=FLAT)
text_n.pack()
all_n = list()
hyperlink = HyperlinkManager(text_n)
figure_n = soup_n.find_all('span',class_="w_tle")
for ha in (figure_n):
all_n.append(ha.a.get_text())
all_n.append(ha.a.get('href'))
for p in range(0, len(all_n), 2):
text_n.insert(INSERT, all_n[p])
text_n.insert(INSERT, "\n")
text_n.insert(INSERT, "Click to read the article..",
hyperlink.add(partial(webbrowser.open, all_n[p + 1])))
text_n.insert(INSERT, "\n")
text_n.insert(INSERT, "\n")
if(name == 'et'):
section = Toplevel()
section.title('Entertainment section')
section.geometry('1200x675+0+0')
img = Image.open('images/entertainment.jpg')
img = img.resize((1200, 675), Image.Resampling.LANCZOS)
self.photobg = ImageTk.PhotoImage(img)
s = Label(section, image=self.photobg)
s.place(x=0, y=0)
url_et = "https://timesofindia.indiatimes.com/entertainment"
content_request_et = requests.get(url_et)
html_content_et = content_request_et.content
soup_et = BeautifulSoup(html_content_et, 'html.parser')
w_frame = LabelFrame(section, bd=20, bg="white", relief=FLAT, text="Entertainment news --------\n",
font=('calibri', 20))
w_frame.place(x=120, y=80, width=900, height=600)
text_et = Text(w_frame, width=800, height=595, font=('calibri', 14), blockcursor=True, relief=FLAT)
text_et.pack()
all_et = list()
hyperlink = HyperlinkManager(text_et)
figure_et = soup_et.find_all('li',class_="small_video_lft clearfix")
for ha in (figure_et):
all_et.append(ha.a.get_text())
all_et.append(ha.a.get('href'))
for p in range(0, len(all_et), 2):
text_et.insert(INSERT, all_et[p])
text_et.insert(INSERT, "\n")
text_et.insert(INSERT, "Click to read the article..",
hyperlink.add(partial(webbrowser.open, all_et[p + 1])))
text_et.insert(INSERT, "\n")
text_et.insert(INSERT, "\n")
if (name == 'e'):
section = Toplevel()
section.title('Education section')
section.geometry('1200x675+0+0')
img = Image.open('images/education.jpg')
img = img.resize((1200, 675), Image.Resampling.LANCZOS)
self.photobg = ImageTk.PhotoImage(img)
e = Label(section, image=self.photobg)
e.place(x=0, y=0)
url_e = "https://timesofindia.indiatimes.com/education/exams"
content_request_e = requests.get(url_e)
html_content_e = content_request_e.content
soup_e = BeautifulSoup(html_content_e, 'html.parser')
w_frame = LabelFrame(section, bd=20, bg="white", relief=FLAT, text="Education news --------\n",
font=('calibri', 20))
w_frame.place(x=120, y=80, width=900, height=600)
text_e = Text(w_frame, width=800, height=595, font=('calibri', 14), blockcursor=True, relief=FLAT)
text_e.pack()
all_e = list()
hyperlink = HyperlinkManager(text_e)
figure_e = soup_e.find_all('figure')
for ha in (figure_e):
if (ha.figcaption != None):
all_e.append(ha.figcaption.get_text())
all_e.append(ha.a.get('href'))
for p in range(0, len(all_e), 2):
text_e.insert(INSERT, all_e[p])
text_e.insert(INSERT, "\n")
text_e.insert(INSERT, "Click to read the article..",
hyperlink.add(partial(webbrowser.open, all_e[p + 1])))
text_e.insert(INSERT, "\n")
text_e.insert(INSERT, "\n")
p1 = Button(Boardframe, text = 'Business',bg='papaya whip',cursor='hand2' ,relief=FLAT, foreground='black', width=15, height=2,activebackground='orange', command=lambda : popular_searches('b')).place(x=40, y= 50)
p2 = Button(Boardframe, text = 'Technology', bg='peach puff',cursor='hand2',relief=FLAT, foreground='black', width=15, height=2,activebackground='orange', command=lambda: popular_searches('t')).place(x=170, y= 50)
p3 = Button(Boardframe, text = 'Sports', bg='lavender',cursor='hand2',relief=FLAT, foreground='black', width=15, height=2,activebackground='orange', command=lambda : popular_searches('s')).place(x=300, y= 50)
p4 = Button(Boardframe, text = 'News', bg='lavender',cursor='hand2',relief=FLAT, foreground='black', width=15, height=2,activebackground='orange', command=lambda : popular_searches('n')).place(x=430, y= 50)
p5 = Button(Boardframe, text = 'Entertainment', bg='cadet blue',cursor='hand2',relief=FLAT, foreground='black', width=15, height=2,activebackground='orange', command=lambda:popular_searches('et')).place(x=560, y= 50)
p6 = Button(Boardframe, text = 'Education', bg='#116562',cursor='hand2',relief=FLAT, foreground='black', width=15, height=2,activebackground='orange', command=lambda:popular_searches('e')).place(x=690, y= 50)
# submit button
btn = Button(Boardframe, text = "Search", width=17, height=3 , foreground="black",relief=RAISED,cursor='hand2',background="yellow",command=search).place(x=372, y= 120)
# recent search
def recent_searches():
rs = Toplevel()
rs.title('Recent searches..')
rs.config(bg='DarkOliveGreen4')
rs.geometry('340x470')
b = Label(rs, text = "Your recent searches :", font= 1).grid(row=0,column=0)
l=Listbox(rs,font=('Calibri',14),width=32,height=15,selectmode=SINGLE)
l.grid(row=5,column=0,columnspan=4)
li = analyse.recentanalysis('Searched_news.csv')
for j in range(len(li)):
l.insert(j,li[j])
s=Button(rs, text = 'Search',width=17, height=3 , foreground="black",relief=RAISED,background="gold",command=lambda : search(l.get(l.curselection()))).place(x=70,y=400)
rsbtn = Button(window, text = "Recent Searches",cursor='hand2', width=17, height=3 , foreground="black",relief=RAISED,background="SlateGray4", font = 1,command=lambda : recent_searches()).place(x=480, y= 300)
# Top 10 searches
def topten_searches():
t10 = Toplevel()
t10.title('Top 10 searches on our website searches..')
t10.config(bg='DarkOliveGreen4')
t10.geometry('340x470')
b = Label(t10, text = "Top 10 searches :", font= 1).grid(row=0,column=0)
l=Listbox(t10,font=('Calibri',14),width=32,height=15,selectmode=SINGLE)
l.grid(row=5,column=0,columnspan=4)
li = analyse.toptenanalysis('Searched_news.csv')
for j in range(len(li)):
l.insert(j,li[j])
s=Button(t10, text = 'Search',width=17, height=3 , foreground="black",relief=RAISED,background="gold",command=lambda : search(l.get(l.curselection()))).place(x=70,y=400)
t10btn = Button(window, text = "Top 10 searches on our website",cursor='hand2', width=35, height=2 , foreground="black",relief=RAISED,background="plum3", font = 1,command=lambda : topten_searches()).place(x=390, y= 450)
window = Tk()
img=Image.open('images/web-cr.jpg')
img=img.resize((1200,675),Image.Resampling.LANCZOS)
photobg=ImageTk.PhotoImage(img)
img_l=Label(window,image=photobg).place(x=0,y=0)
player = web_scrapper(window)
window.mainloop()