python&[aiohttp , aiofiles]案例
2022/2/11 11:12:35
本文主要是介绍python&[aiohttp , aiofiles]案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
aiohttp , aiofiles 安装
网址
图片链接
# aiohttp , aiofiles import asyncio import aiohttp import aiofiles """ 复制图片地址 'https://img2.baidu.com/it/u=1395158804,2693158229&fm=253&fmt=auto&app=138&f=JPEG?w=775&h=500', 'https://img0.baidu.com/it/u=1667622076,2227534038&fm=253&fmt=auto&app=138&f=JPEG?w=703&h=500', 'https://img0.baidu.com/it/u=350325469,2139949192&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=591', 'https://img2.baidu.com/it/u=1028008341,3813970040&fm=253&fmt=auto&app=138&f=JPEG?w=738&h=500', """ async def download(url): print("开始下载",url) # file_name = url.split("/")[-1] # 相当于requests async with aiohttp.ClientSession() as session: # 发送网络请求 async with session.get(url) as resp: # await resp.text() # => resp.text content = await resp.content.read() # =>resp.content # await resp.json() # 写入文件 # f = open("top250.csv",mode='w',encoding='utf-8') async with aiofiles.open("123.csv",mode='wb',encoding='utf-8') as f: await f.write(content) print("下载完成",url) async def main(): url_list=[ 'https://img2.baidu.com/it/u=1395158804,2693158229&fm=253&fmt=auto&app=138&f=JPEG?w=775&h=500', 'https://img0.baidu.com/it/u=1667622076,2227534038&fm=253&fmt=auto&app=138&f=JPEG?w=703&h=500', 'https://img0.baidu.com/it/u=350325469,2139949192&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=591', 'https://img2.baidu.com/it/u=1028008341,3813970040&fm=253&fmt=auto&app=138&f=JPEG?w=738&h=500', ] tasks = [] for url in url_list: t = asyncio.create_task(download(url)) tasks.append(t) await asyncio.wait(tasks) if __name__ == '__main__': asyncio.run(main())
运行结果
这篇关于python&[aiohttp , aiofiles]案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门