1 Star 2 Fork 1

MRlin / ks-python-devidng-utility

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
high-speed-download.py 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
MRlin 提交于 2022-10-25 14:41 . 高速下载图片
import requests,asyncio,aiohttp
def request_zip(url):
rc = requests.get(url)
# 请求链接后保存到链接变量 rc 中
with open("pictures_new/ownload_test.jpg", 'wb') as fout:
# rc.content 写入文件
fout.write(rc.content)
def download_bigData(url):
# stream = True 设置为流读取
rg = requests.get(url, stream=True)
with open("pictures_new/Download_bigTest.jpg", "wb") as fout:
for chunk in rg.iter_content(chunk_size=1024*4):
# 以256个字节为一块,读取文件
if chunk:
# 如果chunk不为空
fout.write(chunk)
async def job(session, url):
# 声明为异步函数
name = 'pictures_new/'+url.split('/')[-1]
# 获得名字
img = await session.get(url)
# 触发到await就切换,等待get到数据
imgcode = await img.read()
# 读取内容
with open(str(name), "wb") as fout:
# 写入文件
fout.write(imgcode)
return str(url)
async def main(loop, url):
async with aiohttp.ClientSession() as session:
# 建立会话 session
tasks = [loop.create_task(job(session, url[_])) for _ in range(2)]
# 建立所有任务
finshed, unfinshed = await asyncio.wait(tasks)
# 触发await,等待任务完成
all_results = [r.result() for r in finshed]
# 获取所有结果
print("ALL RESULTS:" + str(all_results))
if __name__ == '__main__':
# url = "https://pic3.zhimg.com/80/v2-9add4b1033b586a16afd567647326441_720w.jpg"
# request_zip(url)
# download_bigData(url)
urllst = ["https://pic3.zhimg.com/80/v2-999dedf65c41973c21e05e435c2ee536_720w.jpg",
"https://picb.zhimg.com/80/v2-3dc4b27d86e5b870074898cf6532efee_720w.jpg"]
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop, urllst))
Python
1
https://gitee.com/p-kreas/ks-python-devidng-utility.git
git@gitee.com:p-kreas/ks-python-devidng-utility.git
p-kreas
ks-python-devidng-utility
ks-python-devidng-utility
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891