import asyncio
from asyncio import tasks
import aiohttp,aiofiles
import os,time
headers={
'user-agent':r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
}
async def wirte_demo(cot,j):
if not os.path.exists('盲盒'):
os.makedirs('盲盒')
async with aiofiles.open(f'盲盒/{j}.jpg',"wb") as fp:
await fp.write(cot)
print(f"已采集{j}张")
async def open_bbox(session,j,sem):
url=f'https://iw233.cn/api/Random.php'
async with sem:
try:
async with session.request('GET',url,headers=headers) as res:
code=res.status
if code!=200:
print(f'状态码错误:{code}')
return
cot=await res.read()
if len(cot)<1000:
print(f'内容长度过短{len(cot)}')
await wirte_demo(cot,j)
except Exception as e:
print(f'出现错误{e}')
async def main():
sem= asyncio.Semaphore(10)
conn = aiohttp.TCPConnector(limit=10,ssl=False) #设置连接属性
async with aiohttp.ClientSession(connector=conn,trust_env=True) as session:
tasks=[asyncio.create_task(open_bbox(session,j,sem)) for j in range(1000)]
await asyncio.gather(*tasks)
if __name__=='__main__':
start=time.time()
loop=asyncio.get_event_loop()
loop.run_until_complete(main())
end=time.time()
print(f'程序耗时{end-start:.2f}秒')