-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapper_cleaner_2.py
More file actions
316 lines (264 loc) · 9.65 KB
/
scrapper_cleaner_2.py
File metadata and controls
316 lines (264 loc) · 9.65 KB
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
"""
TODO:
I want to restructure this in a much cleaner way to include:
* Use asyncio/threads in order to perform the requests
* Wrap the code for LeMonde alone (I want to get the data from other sources as well)
* Store the whole articles, with the relevant metadata.
* Extract the dates of the articles as well
"""
# from typing import final
from ntpath import join
import requests
from bs4 import BeautifulSoup
import json
import asyncio
import uuid
import configparser
SECRETS = configparser.ConfigParser()
SECRETS.read("./config/auth_lemonde.ini")
GET_FRESH = True
# GET_FRESH = False
DIRECTORY = "./data_4"
all_type_of_links = {
"articles_pages": {
"https://www.lemonde.fr/sciences/",
"https://www.lemonde.fr/sciences/2/",
"https://www.lemonde.fr/idees/",
"https://www.lemonde.fr/idees/2/",
"https://www.lemonde.fr/societe/",
"https://www.lemonde.fr/societe/2/",
"https://www.lemonde.fr/planete/",
"https://www.lemonde.fr/planete/2/",
"https://www.lemonde.fr/politique/",
"https://www.lemonde.fr/politique/2/",
"https://www.lemonde.fr/election-presidentielle-2022/",
"https://www.lemonde.fr/election-presidentielle-2022/2/",
"https://www.lemonde.fr/politique/",
"https://www.lemonde.fr/politique/2/",
# New topics
"https://www.lemonde.fr/medecine/",
"https://www.lemonde.fr/medecine/2/",
"https://www.lemonde.fr/sante/",
"https://www.lemonde.fr/sante/2/",
"https://www.lemonde.fr/campus/",
"https://www.lemonde.fr/campus/2/",
"https://www.lemonde.fr/economie/",
"https://www.lemonde.fr/economie/2/",
"https://www.lemonde.fr/emploi/",
"https://www.lemonde.fr/emploi/2/",
"https://www.lemonde.fr/mathematiques/",
"https://www.lemonde.fr/mathematiques/2/",
"https://www.lemonde.fr/cosmos/",
"https://www.lemonde.fr/cosmos/2/",
"https://www.lemonde.fr/afrique/",
"https://www.lemonde.fr/afrique/2/",
"https://www.lemonde.fr/culture/",
"https://www.lemonde.fr/culture/2/",
"https://www.lemonde.fr/election-presidentielle-2012/",
"https://www.lemonde.fr/election-presidentielle-2012/2/",
"https://www.lemonde.fr/election-presidentielle-2017/",
"https://www.lemonde.fr/election-presidentielle-2017/2/",
"https://www.lemonde.fr/le-monde-des-religions/",
"https://www.lemonde.fr/le-monde-des-religions/2/",
}, # pages with articles inside
"articles_links": set(),
}
seen_articles_pages = set()
seen_articles_links = set()
# URL = "https://www.lemonde.fr/sciences/"
# page = requests.get(URL)
# soup = BeautifulSoup(page.content, "html.parser")
# print(soup.title)
sess = None
async def get_article_content(article_url):
global sess
# page = requests.get(article_url)
print(f"Before the get: {article_url}")
page = sess.get(article_url)
# print(f"After the GET: {article_url}")
soup = BeautifulSoup(page.content, "html.parser")
# soup.str
img_links = []
img_captions = []
all_href = []
all_href_text = []
# print(soup.find_all("figure"))
for item in soup.find_all("figure"):
# print(item)
link = item.find("img")
caption = item.find("figcaption")
# print(caption.text)
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:
print(item)
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 = {
"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_article_links_in_the_page(page):
all_links = page.find_all("a", class_="teaser__link")
for link in all_links:
# print(link.get("href"))
all_type_of_links["articles_links"].add(link.get("href"))
def get_categories(page):
all_category_links = page.find_all("a", class_="page__button")
for link in all_category_links:
# print(link.get("href"))
all_type_of_links["articles_pages"].add(link.get("href"))
all_type_of_links["articles_pages"].add(link.get("href") + "2")
async def main():
# Get all possible article pages
global all_type_of_links
index = 0
if GET_FRESH:
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"{DIRECTORY}/all_links_recorded.json", "r") as file_handle:
all_type_of_links = json.load(file_handle)
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"])
# print(f"All collected links: {all_type_of_links['articles_links']}")
with open(f"{DIRECTORY}/all_links_recorded.json", "w") as file_handle:
json.dump(all_type_of_links, file_handle)
await asyncio.gather(
*[
get_contents_of_articles(link_index, link)
for link_index, link in enumerate(list(all_type_of_links["articles_links"]))
]
)
# for link_index, link in enumerate(list(all_type_of_links["articles_links"])):
# await get_contents_of_articles(link_index, link)
# get_contents_of_articles()
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)
page = sess.get(article_page)
soup = BeautifulSoup(page.content, "html.parser")
get_article_links_in_the_page(soup)
get_categories(soup)
async def get_contents_of_articles(articles_index, articles_link):
# Get all articles content
if articles_link not in seen_articles_links:
seen_articles_links.add(articles_link)
# print(articles_link)
print(
f"Article Index: {articles_index} / {len(all_type_of_links['articles_links'])}"
)
article_content = await get_article_content(articles_link)
with open(f"{DIRECTORY}/{articles_index}.json", "w") as file_handle:
json.dump(
article_content,
file_handle,
)
print("/*" * 50)
if __name__ == "__main__":
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")
# payload['csrf_token'] = signin.find('input', id='csrf_token')['value']
# print(signin.text)
# print("/*-" * 100)
res = sess.post("https://secure.lemonde.fr/sfuser/connexion", data=payload)
success = BeautifulSoup(res._content, "html.parser")
# print(success.text)
try:
asyncio.run(main())
except:
pass
sess.close()