Python 爬虫-大文件下载,带进度条代码

从Json文件中读取内容,遍历Json对象,将下载地址提取出来,注入异步下载函数。

数据源:

{
  "FangZhengShuSong_Regular": {
    "type": "2",
    "code": "fangzhengshusong",
    "name": "方正书宋",
    "preview": "https://wordshub.github.io/free-font/images/font/fangzhengshusong/font.svg",
    "posters": [
      "images/font/fangzhengshusong/poster.svg"
    ],
    "born": "20191225",
    "version": "",
    "license": "商免",
    "auth": true,
    "source": "https://www.foundertype.com/index.php/About/bookAuth/key/my_sysq.html",
    "download": "https://raw.githubusercontent.com/wordshub/free-font/master/assets/font/%E4%B8%AD%E6%96%87/%E6%96%B9%E6%AD%A3%E5%AD%97%E4%BD%93%E7%B3%BB%E5%88%97/%E6%96%B9%E6%AD%A3%E4%B9%A6%E5%AE%8B%E7%AE%80%E4%BD%93.ttf",
    "desc": "「方正书宋」源自上海印刷技术研究所的宋二,原字形距今已有近60年。最初的设计就是专门针对最常用的小字号正文排版。方正书宋的字形端正清秀,中宫适度。凭着清晰爽目、久读不易疲劳、印刷适性好的优点,长期被作为杂志和书籍的正文选字体,所以也是一款非常经典的正文宋体。"
  },
  "FangZhengFangSong_Regular": {
    "type": "2",
    "code": "fangzhengfangsong",
    "name": "方正仿宋",
    "preview": "https://wordshub.github.io/free-font/images/font/fangzhengfangsong/font.svg",
    "posters": [
      "images/font/fangzhengfangsong/poster.svg"
    ],
    "license": "商免",
    "auth": true,
    "source": "https://www.foundertype.com/index.php/About/bookAuth/key/my_sysq.html",
    "download": "https://raw.githubusercontent.com/wordshub/free-font/master/assets/font/%E4%B8%AD%E6%96%87/%E6%96%B9%E6%AD%A3%E5%AD%97%E4%BD%93%E7%B3%BB%E5%88%97/%E6%96%B9%E6%AD%A3%E4%BB%BF%E5%AE%8B%E7%AE%80%E4%BD%93.ttf",
    "desc": "「仿宋体」产生于20世纪初,由当时的铅字制作者仿宋版书中的瘦细字体而制成。方正仿宋源于铅字时代的字稿,其字身略窄,笔画瘦硬,横竖笔画等粗,起笔处有斜势棱角,折笔处棱角分明,整体字形挺拔俊秀,给人以悦目之感。因清晰美观,容易辨认,长期被用于工程图纸文字和书刊中的引文。"
  }

。。。

废话不多说,上代码:

import json
import asyncio
import aiohttp
import urllib.parse
import os
from tqdm.asyncio import tqdm

basedir = 'd:/phpweb/vue-fabric-editor-main/src/assets/fonts/'
savebase = 'd:/phpweb/vue-fabric-editor-main/public/fontFile/'

async def download_file(url, save_path):
    if not os.path.exists(save_path) or os.path.getsize(save_path) < 1024*1024:
        try:
            async with aiohttp.ClientSession() as session:
                async with session.get(url) as resp:
                    total_size = int(resp.headers.get('Content-Length', 0))
                    print(f'Download: {url}')
                    with open(save_path, 'wb') as fd:
                        with tqdm(total=total_size, desc=save_path, unit='B', unit_scale=True) as pbar:
                            async for chunk in resp.content.iter_chunked(1024):
                                fd.write(chunk)
                                pbar.update(len(chunk))
                    await asyncio.sleep(1)
            return save_path
        except Exception as e:
            print(f"Error downloading {url}: {e}")
            return None
    else:
        print(f"File Exist {save_path}")
        return None

async def main():
    json_file = basedir + 'free-font.json'
    with open(json_file, 'r', encoding='UTF-8') as f:
        fonts = json.load(f)
    print(f'Source: {json_file}')
    for key, val in fonts.items():
        font_savefile = urllib.parse.unquote(val['download'].replace('https://raw.githubusercontent.com/wordshub/free-font/master/', savebase))
        if not os.path.exists(os.path.dirname(font_savefile)):
            os.makedirs(os.path.dirname(font_savefile))
        await download_file(val['download'], font_savefile)

if __name__ == "__main__":
    asyncio.run(main())

自行修改参数。
运行效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锐昆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值