-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-scrpaer.py
37 lines (29 loc) · 1.01 KB
/
web-scrpaer.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
import asyncio
import os
import time
import aiohttp
def write_file(data):
with open(f'images/{time.time_ns()}.jpeg', 'wb') as f:
f.write(data)
async def get_page_content(url: str, session):
async with session.get(url, allow_redirects=True) as response:
data = await response.read()
write_file(data)
async def get_web_pages_data(urls: list[str]):
tasks = list()
async with aiohttp.ClientSession() as session:
for i, url in enumerate(urls):
task = asyncio.ensure_future(
get_page_content(url, session)
)
tasks.append(task)
if i % 20 == 0:
await asyncio.gather(*tasks)
tasks = list()
await asyncio.gather(*tasks)
if __name__ == '__main__':
if not os.path.exists('images'):
os.mkdir('images')
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
urls = ['https://loremflickr.com/320/240'] * 100
asyncio.run(get_web_pages_data(urls))