这是我爬取樱花动漫视频的源代码,我用了asyncio异步协程,因为樱花的视频是分片段的,下载要下载几百个视频片段,我用协程进行下载,但是到最后下载的很慢,都会丢失几个视频不下载,而且会请求超时,解码是也是一样,都会丢失几个,一直搞不明白,不知道怎么回事?希望大神们能帮下忙,看看是代码有问题,还是编译器的事,或者樱花的事?我编译器一直用的idle版本是3.10.1
import requests
import asyncio
import aiohttp
import aiofiles
import json
import os
import re
from Crypto.Cipher import AES
if not os.path.exists('video'):
os.mkdir('video')
if not os.path.exists('dec_video'):
os.mkdir('dec_video')
headers ={
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Mobile Safari/537.36',
}
def get_m3u8_text(url):
with requests.Session() as session:
with session.get(url=url,headers=headers)as resp:
with open('租借女友.m3u8','wb') as fp:
fp.write(resp.content)
return session
async def get_m3u8_url():
async with aiofiles.open('租借女友.m3u8','r') as fp:
tasks = []
n = 0
async for url in fp:
if url.startswith('#'):
continue
url = url.strip()
task = asyncio.create_task(get_ts(url,n))
n = n + 1
tasks.append(task)
await asyncio.wait(tasks)
async def get_ts(url,n):
name = f'video{n}.ts'
async with aiohttp.ClientSession() as session:
async with session.get(url=url,headers=headers) as resp:
down_ts = await resp.content.read()
async with aiofiles.open(f'video/{name}','ab') as fp:
await fp.write(down_ts)
print(f'{name}下载完成!!!')
def get_key_url():
with open('租借女友.m3u8','r') as fp:
m3u8_text = fp.read()
async def get_key_dec(key):
tasks = []
for line in range(502):
name = f'video{line}.ts'
tasks.append(asyncio.create_task(get_dec_ts(name,key)))
await asyncio.wait(tasks)