MegaMU个人站

欢迎光临

落日黄沙 白帆秋水 你可知谁的记忆在时空里飞?


福利:盲盒脚本

目录

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}秒')

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦