-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaiocrawler.py
256 lines (212 loc) · 8.06 KB
/
aiocrawler.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
'''
A webcrawler built on asyncio and aiohttp
'''
import asyncio
import logging
from dataclasses import dataclass
from urllib.parse import urljoin, urlparse
from typing import Set, Iterable, List, Tuple, Dict, Optional
from aiohttp import ClientSession, ClientResponseError, ClientTimeout
from aiohttp import ClientConnectionError, ClientPayloadError
from aiohttp.client_exceptions import TooManyRedirects
from bs4 import BeautifulSoup # type: ignore
from bs4.element import Tag # type: ignore
logger = logging.getLogger('AIOCrawler')
class InvalidContentTypeError(Exception):
'''
Exception raised when response content type is not in allowed types
'''
def __init__(self, response):
self.response = response
@dataclass
class TaskQueueMessage:
url: str
depth: int
retry_count: int
class AIOCrawler:
'''
Crawler baseclass that concurrently crawls multiple pages till provided depth
Built on asyncio
'''
timeout: int = 30
max_redirects: int = 10
valid_content_types: Set[str] = {
'text/html',
'text/xhtml',
'application/xhtml+xml',
'application/xhtml',
'application/html',
}
def __init__(
self,
init_url: str,
depth: int = 1,
concurrency: int = 100,
max_retries: int = 2,
user_agent: str = 'AIOCrawler',
) -> None:
'''
Initialize State
'''
self.init_url = init_url
self.depth = depth
self.concurrency = concurrency
self.max_retries = max_retries
self.user_agent = user_agent
self.base_url: str = '{}://{}'.format(
urlparse(self.init_url).scheme, urlparse(self.init_url).netloc
)
self.crawled_urls: Set[str] = set()
self.results: List = []
self.session: Optional[ClientSession] = None
self.task_queue: Optional[asyncio.Queue] = None
async def _make_request(self, url: str) -> str:
'''
Wrapper on aiohttp to make get requests on a shared session
'''
if not self.session:
self.session = ClientSession()
logging.debug(f'Fetching: {url}')
headers = {'User-Agent': self.user_agent}
timeout = ClientTimeout(total=self.timeout)
async with self.session.get(
url,
headers=headers,
raise_for_status=True,
timeout=timeout,
max_redirects=self.max_redirects,
) as response:
if response.content_type not in self.valid_content_types:
raise InvalidContentTypeError(response)
html = await response.text()
return html
def normalize_urls(self, urls: Iterable[Tag]) -> Set[str]:
'''
Normalizes passed urls - Adds domain to relative links
and ignores links from other domains
'''
links = {
urljoin(self.base_url, url['href'])
for url in urls
if urljoin(self.base_url, url['href']).startswith(self.base_url)
}
return links
def find_links(self, html: str) -> Set[str]:
'''
Finds all the links in passed html
'''
soup = BeautifulSoup(html, 'html.parser')
links = self.normalize_urls(soup.select('a[href]'))
return links
def parse(self, url: str, links: Set[str], html: str):
raise NotImplementedError(
'{}.parse callback is not defined'.format(self.__class__.__name__)
)
async def crawl_page(self, url: str) -> Tuple[str, Set[str], str]:
'''
Request a webpage and return all relevant data from it
'''
html = await self._make_request(url)
links = self.find_links(html)
return url, links, html
async def retry_task(self, task):
'''
Retries a task if max retries not hit
'''
if task.retry_count < self.max_retries:
self.crawled_urls.discard(task.url)
task_message = TaskQueueMessage(task.url, task.depth, task.retry_count + 1)
await self.task_queue.put(task_message)
else:
logger.error(f'Max retries exceeded for url: {task.url}')
async def worker(self) -> None:
'''
Pops a url from the task queue and crawls the page
'''
while True:
if not self.task_queue:
break
task = await self.task_queue.get()
logger.debug(f'Working on {task.url} at {task.depth}')
if (task.depth >= self.depth) or (task.url in self.crawled_urls):
self.task_queue.task_done()
logger.debug('Max depth reached')
continue
self.crawled_urls.add(task.url)
try:
url, links, html = await self.crawl_page(task.url)
except InvalidContentTypeError as excp:
logger.error(
f'Non html content type received in response at url: {task.url}'
)
except TooManyRedirects as excp:
logger.error(f'Redirected too many times at url: {task.url}')
except ClientPayloadError as excp:
logger.error(f'Invalid compression or encoding at url: {task.url}')
except asyncio.TimeoutError as excp:
logger.error(f'Timeout at url: {task.url}, retrying ....')
await self.retry_task(task)
except ClientResponseError as excp:
if excp.status > 499:
logger.error(f'Server error at url: {task.url}, retrying ....')
await self.retry_task(task)
else:
logger.error(
f'Client error with status: {excp.status} at url: {task.url}'
)
except ClientConnectionError as excp:
logger.error(f'Connection error at url: {task.url}, retrying ....')
await self.retry_task(task)
except Exception as excp:
logger.error(f'Unhandled exception: {type(excp)} {excp}')
else:
self.results.append(self.parse(url, links, html))
for link in links.difference(self.crawled_urls):
task_message = TaskQueueMessage(link, task.depth + 1, 0)
await self.task_queue.put(task_message)
finally:
self.task_queue.task_done()
async def crawl(self) -> None:
'''
Starts concurrent workers and kickstarts scraping
'''
self.task_queue = asyncio.Queue()
task_message = TaskQueueMessage(self.init_url, 0, 0)
self.task_queue.put_nowait(task_message)
workers = [asyncio.create_task(self.worker()) for i in range(self.concurrency)]
await self.task_queue.join()
for worker in workers:
worker.cancel()
if self.session:
await self.session.close()
async def get_results(self) -> List:
'''
Run the crawler and return the generated sitemap
'''
await self.crawl()
return self.results
class SitemapCrawler(AIOCrawler):
'''
Subclasses AIOCrawler to generate a sitemap for a given domain.
Call `get_results` to access the sitemap.
'''
def parse(self, url: str, links: Set[str], html: str) -> Tuple[str, Set[str]]:
'''
Return a tuple to create the sitemap
'''
return url, links
if __name__ == '__main__':
import argparse
import pprint
parser = argparse.ArgumentParser()
parser.add_argument('-u', action='store', dest='init_url', type=str)
parser.add_argument('-d', action='store', dest='depth', default=1, type=int)
parser.add_argument('-c', action='store', dest='concurrency', default=100, type=int)
parser.add_argument('-r', action='store', dest='max_retries', default=2, type=int)
results = parser.parse_args()
crawler = SitemapCrawler(
results.init_url, results.depth, results.concurrency, results.max_retries
)
sitemap = asyncio.run(crawler.get_results())
pp = pprint.PrettyPrinter()
pp.pprint(sitemap)