forked from bombsimon/news-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
358 lines (274 loc) · 9.12 KB
/
scraper.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
import json
import requests
from bs4 import BeautifulSoup
import header
class Scraper:
"""
Reader is the base class to extend to implement a new news site.
"""
def __init__(self):
pass
def name(self):
raise NotImplementedError
def url(self):
raise NotImplementedError
def headers(self):
raise NotImplementedError
def source(self):
url_source = requests.get(self.url())
return url_source.text
def print_cli(self):
headers = self.headers()
for news_header in headers:
print(news_header)
print()
class Aftonbladet(Scraper):
"""
Aftonbladet implements a reader for https://aftonbladet.se
Aftonbladet is quite special since all the content of the site that we need
is located as JSON inside a <script> tag where the variable
`window.FLUX_STATE` is set to this content.
By iterating over the <script> tags and looking for the one starting with an
assignment to this variable we can extract the tag content and just read the
JSON. When that's done all we need to do is iterate over the data and store
what's relevant to us.
"""
SCRIPT_TAG_START = "window.FLUX_STATE = "
@classmethod
def name(cls):
return "Aftonbladet"
@classmethod
def url(cls):
"""
The URL for the site.
"""
return "https://aftonbladet.se"
def headers(self):
"""
Return a list of all headers for the site.
"""
soup = BeautifulSoup(self.source(), "html.parser")
script_tag = None
for tag in soup.find_all("script"):
# Skip empty tags
if not tag.contents:
continue
# Check if the tag content starts with the JSON assignment.
if tag.contents[0].startswith(self.SCRIPT_TAG_START):
script_tag = tag.contents[0][len(self.SCRIPT_TAG_START) :]
break
if script_tag is None:
raise Exception("No tags found")
json_content = json.loads(script_tag)
articles = json_content["collections"]
# Always one key - randomized string.
random_key = list(articles.keys())[0]
items = articles[random_key]["contents"]["items"]
result = []
for item_id, item in items.items():
# No more articles if we no longer find the "abse" key.
if "abse" not in item_id:
break
for sub_item in item["items"]:
# We're looking for teasers to fetch.
if sub_item["type"] != "box":
continue
if "clickTracking" not in sub_item:
continue
if "object" not in sub_item["clickTracking"]:
continue
if "name" not in sub_item["clickTracking"]["object"]:
continue
title = sub_item["clickTracking"]["object"]["name"]
text = self._find_text(sub_item)
link = sub_item["clickTracking"]["target"]["url"]
result.append(header.Header(title, text, link,))
return result
def _find_text(self, item):
r = self._traverse_items(item, [])
return r[len(r) - 1]
def _traverse_items(self, item, found):
if "text" in item and "value" in item["text"]:
found.append(item["text"]["value"])
if "items" in item:
for i in item["items"]:
self._traverse_items(i, found)
return found
class DN(Scraper):
@classmethod
def name(cls):
return "Dagens Nyheter"
@classmethod
def url(cls):
"""
The URL for the site.
"""
return "https://dn.se"
def headers(self):
"""
Return a list of all headers for the site.
"""
soup = BeautifulSoup(self.source(), "html.parser")
result = []
for article in soup.find_all("a", class_="teaser"):
result.append(
header.Header(
" ".join(article.h1.get_text().split()),
" ".join(article.p.get_text().split())
if article.p is not None
else None,
"{}{}".format(self.url(), article.get("href")),
False,
)
)
return result
class Expressen(Scraper):
"""
Expressen implements a reader for https://expressen.se
"""
@classmethod
def name(cls):
return "Expressen"
@classmethod
def url(cls):
"""
The URL for the site.
"""
return "https://expressen.se"
def headers(self):
"""
Return a list of all headers for the site.
"""
soup = BeautifulSoup(self.source(), "html.parser")
result = []
for article in soup.find_all("div", class_="teaser"):
if article.h2 is None:
continue
result.append(
header.Header(
" ".join(article.h2.get_text().split()),
" ".join(article.p.get_text().split()),
"{}{}".format(self.url(), article.a.get("href")),
False,
)
)
return result
class SVT(Scraper):
"""
SVT implements a reader for https://svt.se
"""
@classmethod
def name(cls):
return "Sveriges Television"
@classmethod
def url(cls):
"""
The URL for the site.
"""
return "https://svt.se"
def headers(self):
"""
Return a list of all headers for the site.
"""
soup = BeautifulSoup(self.source(), "html.parser")
result = []
for article in soup.find_all("article", class_="nyh_teaser"):
textwrapper = article.find("div", class_="nyh_teaser__textwrapper")
result.append(
header.Header(
textwrapper.h1.get_text(),
textwrapper.div.get_text(),
"{}{}".format(self.url(), article.a.get("href")),
False,
)
)
return result
class VK(Scraper):
"""
VK implements a reader for https://vk.se. When the site fetches the news
articles it's made with graphql and a body containing a sha256 hash. I have
yet to figure out how this is calculated but in the meantime a new instance
of VK must be created with the hash. The hash can be found by using the
network inspector in a web browser and looking for a HTTP POST request to
https://content.vk.se/news/vk/graphql. In the body for that request you
should find a similar rbody to the one in this file.
"""
def __init__(self, sha256):
self.sha256hash = sha256
super().__init__()
@classmethod
def name(cls):
return "Västerbottens-Kuriren"
@classmethod
def url(cls):
"""
The URL for the site.
"""
return "https://vk.se"
def headers(self):
"""
Return a list of all headers for the site.
"""
gql = self.source()
result = []
for item in gql["data"]["result"]["hits"]:
teaser = item["teaser"]
result.append(
header.Header(
teaser["title"],
teaser["text"],
"{}{}".format(self.url(), item["urlPath"]),
item["paywall"] == "premium",
)
)
return result
def source(self):
"""
Custom implementation to fetch the source for VK since VK uses GraphQL
which we may use to fetch the articles.
"""
body = {
"operationName": "OCListQueryNonBatched",
"variables": {
"uuid": "7a3463a2-988c-43ba-99f3-39f27fe3d265",
"limit": 100,
"page": 0,
},
"extensions": {
"persistedQuery": {"version": 1, "sha256Hash": self.sha256hash}
},
}
response = requests.post(
"https://content.vk.se/news/vk/graphql",
headers={"Content-Type": "application/json"},
json=body,
)
return response.json()
class Fragbite(Scraper):
"""
Fragbite implements a reader for fragbite.se
News title contains number of comments inside "()"
"""
@classmethod
def url(cls):
"""
The URL for the site.
"""
return "https://fragbite.se"
def headers(self):
"""
Return a list of all headers for the site.
"""
soup = BeautifulSoup(self.source(), "html.parser")
result = []
for article in soup.find_all("div", class_="text"):
subheading = article.find("div", class_="subheading")
result.append(
header.Header(
article.h1.get_text(),
subheading.get_text(),
"{}{}".format(self.url(), article.a.get("href")),
False,
)
)
return result