import os
import asyncio
import aiohttp
import aiofiles
semaphore = asyncio.Semaphore(10)
async def get_file(url, dir, file_name=None, file_suf=None, session=None):
if session is None:
session = aiohttp.ClientSession()
if file_name is None:
file_name = url.split("/")[-1].split(".")[0:-1]
if file_suf is None:
file_suf = url.split("/")[-1].split(".")[-1]
try:
headers = {
"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) ",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-us",
"Connection": "keep-alive",
"Accept-Charset": "GB2312,utf-8;q=0.7,*;q=0.7"}
file_path = r'{}/{}.{}'.format(dir, file_name, file_suf)
if os.path.exists(file_path):
pass
async with semaphore:
async with session.get(url, headers=headers) as res:
res = await res.content.read()
async with aiofiles.open(file_path, 'ab') as f1:
await f1.write(res)
await f1.flush()
print('下载完成:{}'.format(url))
except TypeError:
print('文件链接为空,跳过!')