人工智能学习

爬取图片之aiohttp

本文主要是介绍爬取图片之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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!