-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrap_all_archive.py
372 lines (304 loc) · 10.9 KB
/
scrap_all_archive.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
"""
This version is to scrap the whole archive of LeMonde
"""
# from typing import final
import requests
from bs4 import BeautifulSoup
import json
import asyncio
import uuid
from datetime import datetime, timedelta
import os
import slack_bot
import utilities
import configurations
import argparse
import pathlib
CONFIG = configurations.get_config_file()
SECRETS = configurations.get_secrets_file()
# FIRST_DATE = datetime(day=19, month=12, year=1944)
FIRST_DATE = datetime.strptime(CONFIG["DATES"]["START"], "%d-%m-%Y")
END_DATE = datetime.strptime(CONFIG["DATES"]["END"], "%d-%m-%Y")
GET_FRESH = True
# GET_FRESH = False
all_type_of_links = {
# "articles_pages": {"https://www.lemonde.fr/archives-du-monde/14-01-2020/"},
# "articles_pages": {"https://www.lemonde.fr/archives-du-monde/19-12-1944/"},
"articles_pages": set(),
"archive_link": "https://www.lemonde.fr/archives-du-monde/",
"articles_links": set(),
}
seen_articles_pages = set()
seen_articles_links = set()
sess = None
def generate_whole_years():
global all_type_of_links
global FIRST_DATE
while True:
first_date_str = FIRST_DATE.strftime("%d-%m-%Y")
all_type_of_links["articles_pages"].add(
all_type_of_links["archive_link"] + first_date_str + "/"
)
FIRST_DATE += timedelta(days=1)
if FIRST_DATE > END_DATE:
break
return None
async def get_article_content(article_url):
global sess
# page = requests.get(article_url)
page = sess.get(article_url)
soup = BeautifulSoup(page.content, "html.parser")
img_links = []
img_captions = []
all_href = []
all_href_text = []
for item in soup.find_all("figure"):
link = item.find("img")
caption = item.find("figcaption")
try:
x = link["src"]
y = caption.text
img_links.append(x)
img_captions.append(y)
except:
continue
for item in soup.find_all("a"):
try:
x = item["href"]
y = item.text
all_href.append(x)
all_href_text.append(y)
except:
continue
title = ""
try:
title = soup.find("h1", class_="article__title").text
except:
title = ""
author_name = ""
try:
author_name = soup.find("span", class_="author__name").text
except:
author_name = ""
description = ""
try:
description = soup.find("p", class_="article__desc").text
except:
description = ""
date = ""
try:
date = soup.find("span", class_="meta__date").text
except:
date = ""
reading_time = ""
try:
reading_time = soup.find("p", class_="meta__reading-time").text
except:
reading_time = ""
paragraphs = soup.find_all("p", class_="article__paragraph")
content = ""
for para in paragraphs:
content += para.text + "\n"
article_content = {
"title": title,
"author_name": author_name,
"description": description,
"content": content,
"date": date,
"reading_time": reading_time,
}
other_articles_titles = []
other_articles_links = []
other_articles_desc = []
related_articles_links = soup.find_all("a", class_="teaser__link")
related_articles_desc = soup.find_all("p", class_="teaser__desc")
related_articles_titles = soup.find_all("span", class_="teaser__title")
for item in related_articles_titles:
other_articles_titles.append(item.text)
for item in related_articles_desc:
other_articles_desc.append(item.text)
for item in related_articles_links:
other_articles_links.append(item["href"])
package = {
"page_status": page.status_code,
"article_link": article_url,
"all_href": all_href,
"all_href_text": all_href_text,
"img_links": img_links,
"img_captions": img_captions,
"article_content": article_content,
"other_articles_mentioned": {
"titles": other_articles_titles,
"desc": other_articles_desc,
"links": other_articles_links,
},
}
return package
def get_number_of_pages(article_url, page):
global all_type_of_links
legal = False
all_pages = page.find_all("a", class_="river__pagination--page")
if len(all_pages) > 0:
for page_index, page in enumerate(all_pages):
if page_index == 0:
continue
if ("/" + str(page_index + 1) + "/") in article_url:
legal = True
break
if not legal:
for page_index, page in enumerate(all_pages):
if page_index == 0:
continue
all_type_of_links["articles_pages"].add(
article_url + str(page_index + 1) + "/"
)
def get_article_links_in_the_page(page):
all_links = page.find_all("a", class_="teaser__link")
for link in all_links:
all_type_of_links["articles_links"].add(link.get("href"))
async def main():
# Get all possible article pages
global all_type_of_links
index = 0
if GET_FRESH:
generate_whole_years()
while len(all_type_of_links["articles_pages"]) > 0:
await asyncio.gather(
*[
get_all_links_in_page(article_page)
for article_page in all_type_of_links["articles_pages"]
]
)
all_links_visited = True
for link_0 in all_type_of_links["articles_pages"]:
if link_0 not in seen_articles_pages:
all_links_visited = False
# print(f"Link: {link_0} was not seen")
print(
f'Current number of articles: {len(all_type_of_links["articles_links"])}'
)
print(f'What is left: {len(all_type_of_links["articles_pages"])}')
print(f"What has been seen: {len(seen_articles_pages)}")
print("-------------------------------------")
if all_links_visited:
break
else:
with open(
f"{CONFIG['DIR_PATH']['SRC_DIR']}/all_links_recorded.json", "r"
) as file_handle:
all_type_of_links = json.load(file_handle)
with slack_bot.BasicBot() as my_bot:
my_bot("Scrapping LeMonde: Getting the links is done")
my_bot(
f'Scrapping LeMonde: Current number of articles: {len(all_type_of_links["articles_links"])}'
)
all_type_of_links["articles_pages"] = list(all_type_of_links["articles_pages"])
all_type_of_links["articles_links"] = list(all_type_of_links["articles_links"])
with open(
f"{CONFIG['DIR_PATH']['SRC_DIR']}/all_links_recorded.json", "w"
) as file_handle:
json.dump(all_type_of_links, file_handle)
# See if you need to load the list of seen articles:
if GET_FRESH:
seen_articles_links = set()
else:
seen_articles_links = set(utilities.get_existing_links())
batch_size = int(CONFIG["SCRAP_PARAM"]["BATCH_SIZE"])
print(f"nb of seen articles: {len(seen_articles_links)}")
final_success = True
nb_of_articles_left = len(all_type_of_links["articles_links"]) - len(
seen_articles_links
)
while nb_of_articles_left > 0:
# Make sure the batch size is valid
if nb_of_articles_left < batch_size:
batch_size = nb_of_articles_left
# Build a batch from clean links
links_batch = []
while len(links_batch) < batch_size:
article_link = all_type_of_links["articles_links"].pop()
if article_link not in seen_articles_links:
links_batch.append(article_link)
# Don't go through the rest of the loop if there are no new link in this batch
if len(links_batch) == 0:
print("Nothing new in this batch")
continue
# Perform the requests on those links
success_list = await asyncio.gather(
*[
get_contents_of_articles(link)
for link_index, link in enumerate(links_batch)
]
)
success_set = set(success_list)
# Record those links as 'seen' links
for link in links_batch:
seen_articles_links.add(link)
nb_of_articles_left -= len(links_batch)
# Store the seen links everywhile
try:
with slack_bot.BasicBot() as my_bot:
my_bot(
f"Scrapping LeMonde: {len(seen_articles_links)}/{nb_of_articles_left} articles are done"
)
except:
print("Problem with Slack")
print(
f"{len(seen_articles_links)}/{len(all_type_of_links['articles_links'])} are done!"
)
with slack_bot.BasicBot() as my_bot:
my_bot(f"Scrapping LeMonde: The infernal loop is over.....{final_success}")
async def get_all_links_in_page(article_page):
global sess
if article_page not in seen_articles_pages:
seen_articles_pages.add(article_page)
print(f"article_page: {article_page}")
page = sess.get(article_page)
soup = BeautifulSoup(page.content, "html.parser")
get_article_links_in_the_page(soup)
get_number_of_pages(article_page, soup)
async def get_contents_of_articles(articles_link):
# Get all articles content
success = True
try:
article_content = await get_article_content(articles_link)
if article_content["page_status"] == 200:
with open(
f"{CONFIG['DIR_PATH']['SRC_DIR']}/{str(uuid.uuid4())}.json", "w"
) as file_handle:
json.dump(
article_content,
file_handle,
)
# elif article_content["page_status"] == 404:
# print(
# f'Can\'t find the page: {article_content["page_status"]} -- {articles_link}'
# )
else:
print(
f'Weird response: {article_content["page_status"]} -- {articles_link}'
)
# success = False
except:
print(f"Bad link: {articles_link}")
return success
if __name__ == "__main__":
# This part needs to be cleaned
with slack_bot.BasicBot() as my_bot:
my_bot("Scrapping LeMonde: Started")
if not os.path.isdir(CONFIG["DIR_PATH"]["SRC_DIR"]):
print(f"Folder doesn't exit. Create it")
os.mkdir(CONFIG["DIR_PATH"]["SRC_DIR"])
payload = {
"email": SECRETS["AUTH"]["email"],
"password": SECRETS["AUTH"]["password"],
}
sess = requests.Session()
res = sess.get("https://secure.lemonde.fr/sfuser/connexion")
signin = BeautifulSoup(res._content, "html.parser")
res = sess.post("https://secure.lemonde.fr/sfuser/connexion", data=payload)
success = BeautifulSoup(res._content, "html.parser")
asyncio.run(main())
sess.close()
with slack_bot.BasicBot() as my_bot:
my_bot("Scrapping LeMonde: job complete")