一键下载Python各版本中的最新版

一、背景需求

下载Python的安装包非常简单,只需要去官网就可以了:https://www.python.org/downloads/windows/

但是有时候你可能需要环境测试,需要安装很多版本的Python。

一个一个找倒是也可以,但是我做了个自动筛选的脚本,直接汇总找出各版本Python中的最高子版本的文件下载地址:

import os
import re
from urllib.request import urlopen
from pprint import pprint

import packaging.version as pv

def version(url):
    ver = re.search('python-(\d[\d\.abcr]+\d)', url, re.I).group(1)
    return pv.parse(ver)

pattern = r''
white = ['exe', 'msi']
black = ['embed', 'arm64', 'webinstall']

url = 'https://www.python.org/downloads/windows/'
response = urlopen(url)
text = response.read().decode()

M1 = re.findall(pattern, text, re.I)
M2 = sorted((version(url), url) for url in M1)
M3 = [(ver, url) for ver, url in M2 if any(word in url for word in white) and not any(word in url for word in black)]

keep = {}
for ver, url in M3:
    ver2 = ver.release[:2]
    if ver2 not in keep or ver > keep[ver2]:
        keep[ver2] = ver

M4 = [(ver, url) for ver, url in M3 if ver in keep.values()]

for ver, url in M4:
    # print(f'{str(ver):12}: {url}')
    print(url)

其中需要安装一个版本号解析包,使用以下命令安装:

pip install packaging

二、程序步骤

简单介绍程序的执行步骤,可以根据自己的需求进行修改:

  1. 读取Python官网Windows安装包页面源代码。
  2. 匹配出所有可访问的下载路径。
  3. 用正则表达式取出程序文件名中的版本号,其中Python的版本号存在2位和3位数字、和a/b/rc3中预发布版本。
  4. 用packaging库解析版本号,并根据版本号排序。
  5. 按照黑名单(black)和白名单(white)模式筛选,其中只保留 exemsi 格式的附件,并排除嵌入式版、ARM版、和网络版安装包。
  6. 使用一个字典(keep)保存Python各个版本中的最高小版本号,遍历所有版本号并不断更新最大值,即可得到Python各版本的最高版本号。
  7. 按照最高版本号取出符合条件的下载地址。
  8. 打印输出匹配版本号和下载链接。

下载的事情就交给更专业的软件吧,比如迅雷什么的。

三、下载地址

2023年11月26日运行结果,可以使用迅雷直接下载:

https://www.python.org/ftp/python/2.0.1/Python-2.0.1.exe
https://www.python.org/ftp/python/2.1.3/Python-2.1.3.exe
https://www.python.org/ftp/python/2.2.3/Python-2.2.3.exe
https://www.python.org/ftp/python/2.3.5/Python-2.3.5.exe
https://www.python.org/ftp/python/2.4.4/python-2.4.4.msi
https://www.python.org/ftp/python/2.5.4/python-2.5.4.amd64.msi
https://www.python.org/ftp/python/2.5.4/python-2.5.4.msi
https://www.python.org/ftp/python/2.6.6/python-2.6.6.amd64.msi
https://www.python.org/ftp/python/2.6.6/python-2.6.6.msi
https://www.python.org/ftp/python/2.7.18/python-2.7.18.amd64.msi
https://www.python.org/ftp/python/2.7.18/python-2.7.18.msi
https://www.python.org/ftp/python/3.0.1/python-3.0.1.amd64.msi
https://www.python.org/ftp/python/3.0.1/python-3.0.1.msi
https://www.python.org/ftp/python/3.1.4/python-3.1.4.amd64.msi
https://www.python.org/ftp/python/3.1.4/python-3.1.4.msi
https://www.python.org/ftp/python/3.2.5/python-3.2.5.amd64.msi
https://www.python.org/ftp/python/3.2.5/python-3.2.5.msi
https://www.python.org/ftp/python/3.3.5/python-3.3.5rc1.amd64.msi
https://www.python.org/ftp/python/3.3.5/python-3.3.5rc1.msi
https://www.python.org/ftp/python/3.4.4/python-3.4.4.amd64.msi
https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi
https://www.python.org/ftp/python/3.5.4/python-3.5.4-amd64.exe
https://www.python.org/ftp/python/3.5.4/python-3.5.4.exe
https://www.python.org/ftp/python/3.6.8/python-3.6.8-amd64.exe
https://www.python.org/ftp/python/3.6.8/python-3.6.8.exe
https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64.exe
https://www.python.org/ftp/python/3.7.9/python-3.7.9.exe
https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe
https://www.python.org/ftp/python/3.8.10/python-3.8.10.exe
https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe
https://www.python.org/ftp/python/3.9.13/python-3.9.13.exe
https://www.python.org/ftp/python/3.10.11/python-3.10.11-amd64.exe
https://www.python.org/ftp/python/3.10.11/python-3.10.11.exe
https://www.python.org/ftp/python/3.11.6/python-3.11.6-amd64.exe
https://www.python.org/ftp/python/3.11.6/python-3.11.6.exe
https://www.python.org/ftp/python/3.12.0/python-3.12.0-amd64.exe
https://www.python.org/ftp/python/3.12.0/python-3.12.0.exe
https://www.python.org/ftp/python/3.13.0/python-3.13.0a2-amd64.exe
https://www.python.org/ftp/python/3.13.0/python-3.13.0a2.exe

你可能感兴趣的:(Python,Windows,python)