使用 python 下载 https://unpkg.com/ 中的资源


使用方法

  将代码复制到本地 python 的 ide 中,
  修改 第五行的mod 为要下载的包
  修改 第六行version 为包的版本
  运行即可

环境说明
  需要 requests 依赖包 如果没有 pip install requests

代码

import requests, re, os, time, shutil

url = "https://unpkg.com/"

mod = "axios"
version = "0.21.4"

headers = {'Accept-Language': 'zh-CN,zh;q=0.8',
           'Content-Type': 'text/html;Charset=utf-8',
           "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
           }


# 获取HTML
def getHTML(url, encoding='utf-8'):
    rd = requests.get(url, params=None, headers=headers)
    rd.encoding = encoding
    return rd.text

# 扫描目录
def getPaths(v, p='/', files=[], folders=[]):
    h = getHTML(url + v + p)
    t = re.findall(r'<table(.*?)</table>', h, re.S)[0]
    href = re.findall('href="(.*?)"', t)
    for name in href:
        path = p + name
        # if name in ['../','LICENSE'] or not 'iconfont' in path:#material-design-icons
        if name in ['../', 'LICENSE'] or path in ['/src/', '/packages/', '/types/', '/dist/docs/', '/docs/',
                                                  '/samples/', "/test/", "/locale/"]:  # 跳过
            continue
        print(path)
        if name[-1] == '/':
            folders.append(path)
            getPaths(v, path, files, folders)
        else:
            files.append(path)
    return {"files": files, "folders": folders}


# 创建目录
def makeDirs(dirs, p):
    if p == None:
        p = './'
    for i in dirs:
        path = p + i
        if not os.path.exists(path):
            print("创建目录", path)
            os.makedirs(path)


# 下载文件
def download(url, path=None):  # dir=保存文件夹路径
    if not os.path.exists(path):
        print("下载:", url)
        r = requests.get(url)
        t = str(time.time()) + '.' + str(pid) + '.tmp'
        open(t, 'wb').write(r.content)
        shutil.move(t, path)
    else:
        print("文件已存在")


pid = os.getpid()
print(url + mod + '/')
version = mod + '@' + version
print("默认版本:", version)
paths = getPaths(version)
makeDirs(paths["folders"], version)
for i in paths["files"]:
    u = url + version + i
    download(u, version + '/' + i)
print("完成")

  

转载地址:https://blog.csdn.net/loserccc/article/details/123446361

 

原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/272259.html

(0)
上一篇 2022年7月9日
下一篇 2022年7月9日

相关推荐

发表回复

登录后才能评论