-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapernews.py
409 lines (335 loc) · 14.7 KB
/
scrapernews.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import requests
from bs4 import BeautifulSoup
import webbrowser
from colorama import Fore, Style
print("Let's read the news")
newspapers = ['Financial Times', 'New Yorker', 'Economist', 'New York Times', 'Le Monde', 'The Guardian']
exit_flag = False
should_print_titles = True
while True:
if should_print_titles:
print('_'*10)
print('\n')
for i, paper in enumerate(newspapers):
print(f"{Fore.BLUE}{i + 1} - {Style.RESET_ALL} {Fore.MAGENTA}{paper}{Style.RESET_ALL}")
print('\n')
print('_'*10)
user_input = input(f"{Fore.GREEN}{Style.BRIGHT}Pick a newspaper by number or type 'x' to exit:\n > {Style.RESET_ALL}")
should_print_titles = True
if user_input.lower() == 'x':
print("Goodbye!")
break
try:
user_input = int(user_input) - 1
if not (0 <= user_input < len(newspapers)):
print("Type a valid number")
should_print_titles = False
continue
except ValueError:
print("Type a valid command")
should_print_titles = False
continue
paper = newspapers[user_input]
if paper == 'Financial Times':
print(f'Read the {paper.upper()}')
ft = requests.get("http://www.ft.com")
ftsoup = BeautifulSoup(ft.content, "html.parser")
ftresults = ftsoup.find_all(class_="js-teaser-headline")
ft_links = []
ft_title = []
ft_description = []
for div in ftresults[:10]:
anchor = div.find('a')
if anchor:
href = 'https://www.ft.com' + anchor.get('href')
text = anchor.get_text(strip=True)
if href:
ft_links.append(href)
ft_title.append(text)
should_print_list = True
while True:
if should_print_list:
for i, result in enumerate(ft_title):
print(f"{Fore.BLUE}{i + 1} - {Style.RESET_ALL} {Fore.MAGENTA}{result}{Style.RESET_ALL}")
user_input = input(f"{Fore.GREEN}{Style.BRIGHT}Pick one article, press enter to choose another newspaper, or press x to exit: \n > {Style.RESET_ALL}")
else:
user_input = input(f">")
if user_input.lower() == 'x':
exit_flag = True
break
elif user_input.lower() == '':
break
try:
user_input = int(user_input) - 1
if 0 <= user_input < len(ft_links):
webbrowser.open(ft_links[user_input])
print('Article opened in your browser')
should_print_list = False
else:
print('Number not in list')
should_print_list = False
except ValueError:
print('Invalid command')
should_print_list = False
elif paper == 'New Yorker':
print(f'Read the {paper.upper()}')
ny = requests.get("https://www.newyorker.com/")
nysoup = BeautifulSoup(ny.content, "html.parser")
nyresults = nysoup.find_all(class_="summary-item__hed-link")
ny_links = []
ny_titles = []
ny_descriptions = []
for div in nyresults[:10]:
if div:
href = 'https://www.newyorker.com' + div.get('href')
text = div.get_text(strip=True)
ny_links.append(href)
ny_titles.append(text)
for link in ny_links:
article_page = requests.get(link)
article_soup = BeautifulSoup(article_page.content, 'html.parser')
description_divs = article_soup.find_all(class_=["ContentHeaderDek-bIqFFZ", "SplitScreenContentHeaderDek-emptdL"])
if description_divs:
for div in description_divs:
description = div.get_text(strip=True)
ny_descriptions.append(description)
else:
ny_descriptions.append('Description not found')
should_print_list = True
while True:
if should_print_list:
for i, (title, description) in enumerate(zip(ny_titles, ny_descriptions)):
print(f"{Fore.BLUE}{i + 1} - {Style.RESET_ALL} {Fore.MAGENTA}{title}{Style.RESET_ALL}")
print(f"{Fore.YELLOW} - {Style.RESET_ALL} {Fore.WHITE}{description}{Style.RESET_ALL}")
user_input = input(f"{Fore.GREEN}{Style.BRIGHT}Pick one article, press enter to get to the next newspaper, or press x to exit: \n > {Style.RESET_ALL}")
else:
user_input = input(f">")
if user_input.lower() == 'x':
exit_flag = True
break
elif user_input.lower() == '':
break
try:
user_input = int(user_input) - 1
if 0 <= user_input < len(ny_links):
webbrowser.open(ny_links[user_input])
should_print_list = False
else:
print('Number not in list')
should_print_list = False
except ValueError:
print('Invalid command')
should_print_list = False
elif paper == 'Economist':
print(f'Read the {paper.upper()}')
te = requests.get("https://www.economist.com/")
tesoup = BeautifulSoup(te.content, "html.parser")
teresults = tesoup.find_all(class_="ekfon2k0")
te_links = []
te_titles = []
for div in teresults[:10]:
anchor = div.find('a')
if anchor:
href = 'https://www.economist.com' + anchor.get('href')
text = anchor.get_text(strip=True)
if href:
te_links.append(href)
te_titles.append(text)
should_print_list = True
while True:
if should_print_list:
for i, result in enumerate(te_titles):
print(f"{Fore.BLUE}{i + 1} - {Style.RESET_ALL} {Fore.MAGENTA}{result}{Style.RESET_ALL}")
user_input = input(f"{Fore.GREEN}{Style.BRIGHT}Pick one article, press enter to get to the next newspaper, or press x to exit: \n > {Style.RESET_ALL}")
else:
user_input = input(f">")
if user_input.lower() == 'x':
exit_flag = True
break
elif user_input.lower() == '':
break
try:
user_input = int(user_input) - 1
if 0 <= user_input < len(te_links):
webbrowser.open(te_links[user_input])
should_print_list = False
else:
print('Number not in list')
should_print_list = False
except ValueError:
print('Invalid command')
should_print_list = False
elif paper == 'New York Times':
print(f'Read the {paper.upper()}')
nytimes = requests.get("https://www.nytimes.com/")
nysoup = BeautifulSoup(nytimes.content, "html.parser")
nytimes_results = nysoup.find_all(class_="css-9mylee")
nytimes_links = []
nytimes_titles = []
nytimes_descriptions = []
for div in nytimes_results[:10]:
if div:
nested_div = div.find(class_="css-xdandi")
if nested_div:
text = nested_div.get_text(strip=True)
nytimes_titles.append(text)
href = div.get('href')
if href:
nytimes_links.append(href)
should_print_list = True
while True:
if should_print_list:
for i, result in enumerate(nytimes_titles):
print(f"{Fore.BLUE}{i + 1} - {Style.RESET_ALL} {Fore.MAGENTA}{result}{Style.RESET_ALL}")
user_input = input(f"{Fore.GREEN}{Style.BRIGHT}Pick one article, press enter to choose another newspaper, or press x to exit: \n > {Style.RESET_ALL}")
else:
user_input = input(f">")
if user_input.lower() == 'x':
exit_flag = True
break
elif user_input.lower() == '':
break
try:
user_input = int(user_input) - 1
if 0 <= user_input < len(nytimes_links):
webbrowser.open(nytimes_links[user_input])
should_print_list = False
else:
print('Number not in list')
should_print_list = False
except ValueError:
print('Invalid command')
should_print_list = False
elif paper == 'Le Monde':
print(f'Read {paper.upper()}')
lm = requests.get("https://www.lemonde.fr/")
lmsoup = BeautifulSoup(lm.content, "html.parser")
lm_links = []
lm_titles = []
lm_descs = []
# main header
lmresults = lmsoup.find_all(class_="article--main")
for div in lmresults:
a_tag = div.find('a')
if a_tag:
href = a_tag.get('href')
title_tag = a_tag.find('p', class_='article__title-label')
article_desc = a_tag.find('p', class_='article__desc')
if href:
lm_links.append(href)
lm_titles.append(title_tag.get_text(strip=True))
lm_descs.append(article_desc.get_text(strip=True))
# secondary articles NO DESC
lmresults = lmsoup.find_all(class_="article--headlines")
for div in lmresults:
a_tag = div.find('a')
if a_tag:
href = a_tag.get('href')
title_tag = a_tag.find('p', class_='article__title')
if href:
lm_links.append(href)
lm_titles.append(title_tag.get_text(strip=True))
lm_descs.append('-')
# second row articles
lmresults = lmsoup.find_all(class_="article--runner")
for div in lmresults:
a_tag = div.find('a')
if a_tag:
href = a_tag.get('href')
title_tag = a_tag.find('p', class_='article__title')
article_desc = a_tag.find('p', class_='article__desc')
if href:
lm_links.append(href)
lm_titles.append(title_tag.get_text(strip=True))
lm_descs.append(article_desc.get_text(strip=True))
# side article and selection de la redaction
lmresults = lmsoup.find_all(class_="article--featured")
for div in lmresults:
a_tag = div.find('a')
if a_tag:
href = a_tag.get('href')
title_tag = a_tag.find('p', class_='article__title')
article_desc = a_tag.find('p', class_='article__desc')
if href:
lm_links.append(href)
lm_titles.append(title_tag.get_text(strip=True))
lm_descs.append(article_desc.get_text(strip=True))
# River articles NO DESC
lmresults = lmsoup.find_all(class_="article--river")
for div in lmresults:
a_tag = div.find('a')
if a_tag:
href = a_tag.get('href')
title_tag = a_tag.find('h3', class_='article__title')
if href:
lm_links.append(href)
lm_titles.append(title_tag.get_text(strip=True))
lm_descs.append('-')
should_print_list = True
while True:
if should_print_list:
for i, (title, description) in enumerate(zip(lm_titles, lm_descs)):
print(f"{Fore.BLUE}{i + 1} - {Style.RESET_ALL} {Fore.MAGENTA}{title}{Style.RESET_ALL}")
print(f"{Fore.YELLOW} - {Style.RESET_ALL} {Fore.WHITE}{description}{Style.RESET_ALL}")
user_input = input(f"{Fore.GREEN}{Style.BRIGHT}Pick one article, press enter to choose another newspaper, or press x to exit: \n > {Style.RESET_ALL}")
else:
user_input = input(f">")
if user_input.lower() == 'x':
exit_flag = True
break
elif user_input.lower() == '':
break
try:
user_input = int(user_input) - 1
if 0 <= user_input < len(lm_links):
webbrowser.open(lm_links[user_input])
should_print_list = False
else:
print('Number not in list')
should_print_list = False
except ValueError:
print('Invalid command')
should_print_list = False
elif paper == 'The Guardian':
print(f'Read {paper.upper()}')
tg = requests.get("https://www.theguardian.com/international")
tgsoup = BeautifulSoup(tg.content, "html.parser")
tg_results = tgsoup.find_all(class_="dcr-lv2v9o")
tg_links = []
tg_titles = []
tg_descriptions = []
for div in tg_results[:10]:
if div:
href = div.get('href')
label = div.get('aria-label')
if href:
tg_links.append("https://www.theguardian.com" + href)
tg_titles.append(label)
should_print_list = True
while True:
if should_print_list:
for i, result in enumerate(tg_titles):
print(f"{Fore.BLUE}{i + 1} - {Style.RESET_ALL} {Fore.MAGENTA}{result}{Style.RESET_ALL}")
user_input = input(f"{Fore.GREEN}{Style.BRIGHT}Pick one article, press enter to choose another newspaper, or press x to exit: \n > {Style.RESET_ALL}")
else:
user_input = input(f">")
if user_input.lower() == 'x':
exit_flag = True
break
elif user_input.lower() == '':
break
try:
user_input = int(user_input) - 1
if 0 <= user_input < len(tg_links):
webbrowser.open(tg_links[user_input])
should_print_list = False
else:
print('Number not in list')
should_print_list = False
except ValueError:
print('Invalid command')
should_print_list = False
if exit_flag:
print("Goodbye!")
break
# missing descriptions to FT, Economist, NYT, guardian