爬取图片之aiohttp
作者:互联网
1 import asyncio 2 import aiohttp 3 4 urls = [ 5 "http://kr.shanghai-jiuxin.com/file/mm/20211130/jfehprjyjmy.jpg", 6 "http://kr.shanghai-jiuxin.com/file/mm/20211130/dk1ys3i2ax3.jpg", 7 "http://kr.shanghai-jiuxin.com/file/mm/20211130/veazcucqj3y.jpg", 8 "http://kr.shanghai-jiuxin.com/file/mm/20211130/skajtydcoyj.jpg" 9 ] 10 11 12 async def aiodownload(url): 13 name = url.split("/")[-1] 14 async with aiohttp.ClientSession() as session: # requests 15 async with session.get(url) as resp: # resp = requests.get() 16 # 请求回来,写入文件 17 with open(f"20220626/{name}", mode='wb') as f: 18 f.write(await resp.content.read()) 19 20 print("下载完毕!!!") 21 22 23 async def main(): 24 tasks = [] 25 for url in urls: 26 tasks.append(asyncio.create_task(aiodownload(url))) 27 await asyncio.wait(tasks) 28 29 30 if __name__ == '__main__': 31 loop = asyncio.new_event_loop() 32 asyncio.set_event_loop(loop) 33 loop.run_until_complete(main())
标签:__,aiohttp,url,jpg,爬取,file,图片,loop,asyncio 来源: https://www.cnblogs.com/ang0/p/16414411.html