批量下载 SpigotMC 插件脚本

用 Python 请求 SpigotMC 下载接口批量抓取插件:携带 Cookie 与 Referer 通过校验,从 Content-Disposition 取文件名,单个与整页批量两种写法。

前言

本文介绍如何下载spigotmc插件脚本,并使用命令行运行。
提前准备Cookie

安装python环境

pip install requests
pip install bs4

单个插件下载


import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0',
    'Referer': '历史页面链接',
    'Cookie': 'cf_clearance=******'
}


# 发送GET请求
url = '下载链接'
response = requests.get(url, headers=headers)

if response.status_code == 200:
    # 获取文件名
    content_disposition = response.headers.get('Content-Disposition')
    if content_disposition:
        filename = content_disposition.split('filename=')[1].strip('"')
    else:
        filename = 'downloaded_file.jar'  # 如果服务器未返回文件名,默认使用这个

    # 写入文件
    with open(filename, 'wb') as f:
        f.write(response.content)
    
    print(f"文件 '{filename}' 下载完成。")
else: print(f"下载失败,HTTP响应码:{response.status_code}")

批量插件下载

指定`save_directory`保存的文件夹路径,填写`base_url`为历史页面链接
import os
import requests
from bs4 import BeautifulSoup
import re

# 定义请求头部信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0',
    'Referer': '历史页面链接',
    'Cookie': '******'
}

# 指定文件保存目录
save_directory = r'文件夹路径'

# 获取历史版本页面内容
base_url = '历史页面链接'
response = requests.get(base_url, headers=headers)

if response.status_code != 200:
    print(f"无法获取历史版本页面,HTTP响应码:{response.status_code}")
    exit()

html_content = response.text

# 使用BeautifulSoup解析HTML内容,找到历史版本的下载链接
soup = BeautifulSoup(html_content, 'html.parser')

# 查找所有包含历史版本信息的行<tr>
version_rows = soup.find_all('tr', class_='dataRow')

for row in version_rows:
    # 查找版本号<td>元素
    version_element = row.find('td', class_='version')
    if version_element:
        version = version_element.text.strip()

        # 查找包含下载链接的<td>元素
        download_td = row.find('td', class_='dataOptions download')
        if download_td:
            download_link = download_td.find('a', class_='secondaryContent')['href']
            download_url = f'https://www.spigotmc.org/{download_link}'

            print(f"解析到版本 {version},下载链接:{download_url}")

            try:
                # 发送GET请求下载文件
                response = requests.get(download_url, headers=headers, timeout=60)

                if response.status_code == 200:
                    content_disposition = response.headers.get('Content-Disposition')
                    if content_disposition:
                        filename = content_disposition.split('filename=')[1].strip('"')
                    else:
                        filename = f'CMI-{version}.jar'  # 使用版本号作为默认文件名

                    # 构建完整的文件保存路径
                    file_path = os.path.join(save_directory, filename)

                    # 保存文件到指定路径
                    with open(file_path, 'wb') as f:
                        f.write(response.content)
                    
                    print(f"版本 {version} 下载完成,文件保存至 '{file_path}'")
                else:
                    print(f"版本 {version} 下载失败,HTTP响应码:{response.status_code}")
            except Exception as e:
                print(f"下载版本 {version} 失败:{str(e)}")
        else:
            print(f"版本 {version} 未找到下载链接")
    else:
        print("未找到版本号")

(内容省略,已复制)

评论