forked from fipl-hse/2022-2-level-ctlr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapper.py
305 lines (243 loc) · 8.58 KB
/
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
"""
Crawler implementation
"""
import datetime
import json
import re
import shutil
from pathlib import Path
from typing import Pattern, Union
import requests
from bs4 import BeautifulSoup
from core_utils.article.article import Article
from core_utils.article.io import to_meta, to_raw
from core_utils.config_dto import ConfigDTO
from core_utils.constants import (ASSETS_PATH, CRAWLER_CONFIG_PATH,
NUM_ARTICLES_UPPER_LIMIT,
TIMEOUT_LOWER_LIMIT, TIMEOUT_UPPER_LIMIT)
class IncorrectSeedURLError(Exception):
pass
class NumberOfArticlesOutOfRangeError(Exception):
pass
class IncorrectNumberOfArticlesError(Exception):
pass
class IncorrectHeadersError(Exception):
pass
class IncorrectEncodingError(Exception):
pass
class IncorrectTimeoutError(Exception):
pass
class IncorrectVerifyError(Exception):
pass
class Config:
"""
Unpacks and validates configurations
"""
def __init__(self, path_to_config: Path) -> None:
"""
Initializes an instance of the Config class
"""
self.path_to_config = path_to_config
self._validate_config_content()
self._config_dto = self._extract_config_content()
self._seed_urls = self._config_dto.seed_urls
self._num_articles = self._config_dto.total_articles
self._headers = self._config_dto.headers
self._encoding = self._config_dto.encoding
self._timeout = self._config_dto.timeout
self._should_verify_certificate = self._config_dto.should_verify_certificate
self._headless_mode = self._config_dto.headless_mode
def _extract_config_content(self) -> ConfigDTO:
"""
Returns config values
"""
with open(self.path_to_config, 'r', encoding='utf-8') as f:
config = json.load(f)
return ConfigDTO(
config["seed_urls"],
config["total_articles_to_find_and_parse"],
config["headers"],
config["encoding"],
config["timeout"],
config["should_verify_certificate"],
config["headless_mode"],
)
def _validate_config_content(self) -> None:
"""
Ensure configuration parameters
are not corrupt
"""
config_dto = self._extract_config_content()
if not isinstance(config_dto.seed_urls, list):
raise IncorrectSeedURLError
for url in config_dto.seed_urls:
if not re.match(r"https?://.*/", url) or not isinstance(url, str):
raise IncorrectSeedURLError
total_articles = config_dto.total_articles
if not isinstance(total_articles, int) \
or total_articles < 1:
raise IncorrectNumberOfArticlesError
if total_articles > NUM_ARTICLES_UPPER_LIMIT:
raise NumberOfArticlesOutOfRangeError
if not isinstance(config_dto.headers, dict):
raise IncorrectHeadersError
if not isinstance(config_dto.encoding, str):
raise IncorrectEncodingError
timeout = config_dto.timeout
if not isinstance(timeout, int) or timeout <= TIMEOUT_LOWER_LIMIT or timeout >= TIMEOUT_UPPER_LIMIT:
raise IncorrectTimeoutError
should_verify = config_dto.should_verify_certificate
if not isinstance(should_verify, bool) or not isinstance(config_dto.headless_mode, bool):
raise IncorrectVerifyError
def get_seed_urls(self) -> list[str]:
"""
Retrieve seed urls
"""
return self._seed_urls
def get_num_articles(self) -> int:
"""
Retrieve total number of articles to scrape
"""
return self._num_articles
def get_headers(self) -> dict[str, str]:
"""
Retrieve headers to use during requesting
"""
return self._config_dto.headers
def get_encoding(self) -> str:
"""
Retrieve encoding to use during parsing
"""
return self._encoding
def get_timeout(self) -> int:
"""
Retrieve number of seconds to wait for response
"""
return self._timeout
def get_verify_certificate(self) -> bool:
"""
Retrieve whether to verify certificate
"""
return self._should_verify_certificate
def get_headless_mode(self) -> bool:
"""
Retrieve whether to use headless mode
"""
return self._headless_mode
def make_request(url: str, config: Config) -> requests.models.Response:
"""
Delivers a response from a request
with given configuration
"""
response = requests.get(url, headers=config.get_headers(), timeout=config.get_timeout(),
verify=config.get_verify_certificate())
response.encoding = config.get_encoding()
return response
class Crawler:
"""
Crawler implementation
"""
url_pattern: Union[Pattern, str]
def __init__(self, config: Config) -> None:
"""
Initializes an instance of the Crawler class
"""
self._config = config
self._seed_urls = config.get_seed_urls()
self.urls = []
def _extract_url(self, article_bs: BeautifulSoup) -> str:
"""
Finds and retrieves URL from HTML
"""
href = article_bs.get("href")
if href and href.startswith("https://www.fontanka.ru/") and 'longreads' not in href \
and '__site/about/' not in href:
return str(href)
return ""
def find_articles(self) -> None:
"""
Finds articles
"""
for seed_url in self._seed_urls:
response = make_request(seed_url, self._config)
soup = BeautifulSoup(response.text, "lxml")
for paragraph in soup.find_all('a'):
if len(self.urls) >= self._config.get_num_articles():
return
url = self._extract_url(paragraph)
if not url or url in self.urls:
continue
self.urls.append(url)
def get_search_urls(self) -> list:
"""
Returns seed_urls param
"""
return self._seed_urls
class HTMLParser:
"""
ArticleParser implementation
"""
def __init__(self, full_url: str, article_id: int, config: Config) -> None:
"""
Initializes an instance of the HTMLParser class
"""
self.full_url = full_url
self.article_id = article_id
self._config = config
self.article = Article(self.full_url, self.article_id)
def _fill_article_with_text(self, article_soup: BeautifulSoup) -> None:
"""
Finds text of article
"""
self.article.text = '\n'.join([text.get_text(strip=True) for text in article_soup.find_all('div', class_='CVah B3a1 B3ah')])
def _fill_article_with_meta_information(self, article_soup: BeautifulSoup) -> None:
"""
Finds meta information of article
"""
title = article_soup.find('h1', class_="DHat primaryH4HeadlineMobile primaryH2HeadlineTablet")
if title:
self.article.title = title.text
else:
self.article.title = "NONE"
author = article_soup.find('a', class_="CZk1")
if author:
self.article.author = [author.text]
else:
self.article.author = ["NOT FOUND"]
def unify_date_format(self, date_str: str) -> datetime.datetime:
"""
Unifies date format
"""
pass
def parse(self) -> Union[Article, bool, list]:
"""
Parses each article
"""
response = make_request(self.full_url, self._config)
article = BeautifulSoup(response.text, "lxml")
self._fill_article_with_text(article)
self._fill_article_with_meta_information(article)
return self.article
def prepare_environment(base_path: Union[Path, str]) -> None:
"""
Creates ASSETS_PATH folder if no created and removes existing folder
"""
if base_path.exists():
shutil.rmtree(base_path)
base_path.mkdir(parents=True)
def main() -> None:
"""
Entrypoint for scrapper module
"""
configuration = Config(path_to_config=CRAWLER_CONFIG_PATH)
prepare_environment(ASSETS_PATH)
crawler = Crawler(config=configuration)
crawler.find_articles()
for id_, url in enumerate(crawler.urls, start=1):
parser = HTMLParser(full_url=url, article_id=id_, config=configuration)
article = parser.parse()
if isinstance(article, Article):
to_raw(article)
to_meta(article)
if __name__ == "__main__":
main()