-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathscrapper.py
216 lines (158 loc) · 4.6 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
"""
Crawler implementation.
"""
# pylint: disable=too-many-arguments, too-many-instance-attributes, unused-import, undefined-variable
import pathlib
from typing import Pattern, Union
class Config:
"""
Class for unpacking and validating configurations.
"""
def __init__(self, path_to_config: pathlib.Path) -> None:
"""
Initialize an instance of the Config class.
Args:
path_to_config (pathlib.Path): Path to configuration.
"""
def _extract_config_content(self) -> ConfigDTO:
"""
Get config values.
Returns:
ConfigDTO: Config values
"""
def _validate_config_content(self) -> None:
"""
Ensure configuration parameters are not corrupt.
"""
def get_seed_urls(self) -> list[str]:
"""
Retrieve seed urls.
Returns:
list[str]: Seed urls
"""
def get_num_articles(self) -> int:
"""
Retrieve total number of articles to scrape.
Returns:
int: Total number of articles to scrape
"""
def get_headers(self) -> dict[str, str]:
"""
Retrieve headers to use during requesting.
Returns:
dict[str, str]: Headers
"""
def get_encoding(self) -> str:
"""
Retrieve encoding to use during parsing.
Returns:
str: Encoding
"""
def get_timeout(self) -> int:
"""
Retrieve number of seconds to wait for response.
Returns:
int: Number of seconds to wait for response
"""
def get_verify_certificate(self) -> bool:
"""
Retrieve whether to verify certificate.
Returns:
bool: Whether to verify certificate or not
"""
def get_headless_mode(self) -> bool:
"""
Retrieve whether to use headless mode.
Returns:
bool: Whether to use headless mode or not
"""
def make_request(url: str, config: Config) -> requests.models.Response:
"""
Deliver a response from a request with given configuration.
Args:
url (str): Site url
config (Config): Configuration
Returns:
requests.models.Response: A response from a request
"""
class Crawler:
"""
Crawler implementation.
"""
url_pattern: Union[Pattern, str]
def __init__(self, config: Config) -> None:
"""
Initialize an instance of the Crawler class.
Args:
config (Config): Configuration
"""
def _extract_url(self, article_bs: BeautifulSoup) -> str:
"""
Find and retrieve url from HTML.
Args:
article_bs (bs4.BeautifulSoup): BeautifulSoup instance
Returns:
str: Url from HTML
"""
def find_articles(self) -> None:
"""
Find articles.
"""
def get_search_urls(self) -> list:
"""
Get seed_urls param.
Returns:
list: seed_urls param
"""
# 10
# 4, 6, 8, 10
class HTMLParser:
"""
HTMLParser implementation.
"""
def __init__(self, full_url: str, article_id: int, config: Config) -> None:
"""
Initialize an instance of the HTMLParser class.
Args:
full_url (str): Site url
article_id (int): Article id
config (Config): Configuration
"""
def _fill_article_with_text(self, article_soup: BeautifulSoup) -> None:
"""
Find text of article.
Args:
article_soup (bs4.BeautifulSoup): BeautifulSoup instance
"""
def _fill_article_with_meta_information(self, article_soup: BeautifulSoup) -> None:
"""
Find meta information of article.
Args:
article_soup (bs4.BeautifulSoup): BeautifulSoup instance
"""
def unify_date_format(self, date_str: str) -> datetime.datetime:
"""
Unify date format.
Args:
date_str (str): Date in text format
Returns:
datetime.datetime: Datetime object
"""
def parse(self) -> Union[Article, bool, list]:
"""
Parse each article.
Returns:
Union[Article, bool, list]: Article instance
"""
def prepare_environment(base_path: Union[pathlib.Path, str]) -> None:
"""
Create ASSETS_PATH folder if no created and remove existing folder.
Args:
base_path (Union[pathlib.Path, str]): Path where articles stores
"""
def main() -> None:
"""
Entrypoint for scrapper module.
"""
if __name__ == "__main__":
main()