-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.py
More file actions
75 lines (58 loc) · 2.58 KB
/
03.py
File metadata and controls
75 lines (58 loc) · 2.58 KB
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
import aiohttp
import asyncio
from bs4 import BeautifulSoup
from settings import *
import re
import time
import platform
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
start_time = time.time()
# ссылки на посты, которые нужно открыть
with open(primary_save_file, 'r') as f1, open(secondary_save_file, 'r') as f2:
urls1 = [line.strip() for line in f1]
urls2 = [line.strip() for line in f2]
urls = urls1 + urls2
print('Открываем', len(urls), 'постов')
# куда сохранятся ссылки на скачивание
with open(download_links, 'r', encoding='utf-8') as f:
download_arr = [line.strip() for line in f]
print('Сбор ссылок на скачивание')
async def fetch_url(session, url):
try:
async with session.get(url) as response:
# получить html разметку страницы
html = await response.text()
soup = BeautifulSoup(html, 'html.parser')
# найти элемент, в котором ссылки
script_element = soup.find('script', {'data-relay-response': 'true'})
script_content = script_element.string if script_element else ''
# вытащить из найденного элемента ссылку на файл
download_url = ''
for pattern in patterns:
match = re.findall(pattern, script_content)
if match:
download_url = match[0][7:-1]
break
# сохранить найденную ссылку
if download_url:
download_arr.append(download_url)
except aiohttp.ClientError as e:
print('ошибка', url)
print(e)
async def main():
length = len(urls)
step = 1024 # по столько запросов за раз
# скачивать пока не закончится список
for i in range(0, length, step):
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls[i:i+step]]
await asyncio.gather(*tasks, return_exceptions=True)
print(f'Открыто {min(i+step, length)} из {length}')
asyncio.run(main())
# сохранить
with open(download_links, 'w', encoding='utf-8') as f:
for i in set(download_arr):
print(i, file=f)
print('\nГотово, сохранено', len(set(download_arr)), 'ссылок')
print("%s sec" % round(time.time() - start_time))