Python西瓜视频下载源码(支持单个视频以及合集) 

1、配置python环境,安装相应的库

2、安装ffmpeg,用于音频和视频合并(因为西瓜视频的音频和视频是分开的,需要各自下载,然后合并)

import base64
import json
import os.path
import re
import subprocess
 
import requests
 
headers = {
    "cookie": "ixigua-a-s=0; __ac_nonce=062711eef0074c53fd301; "
              "__ac_signature=_02B4Z6wo00f01ixJMjwAAIDDB8Np-ahvLqYsaTaAAOlxwFWsX1Khk-KvFlM1N2JBoEUa-eVrLxEyF"
              ".bLdz61qjeQ-CTdvApqa7NKn7HTvbu1Nktww-QaXyO3gNQSkIdrfp3XdUkGrdCwrQKpea; "
              "MONITOR_WEB_ID=6364cae8-2aa7-4531-9a30-1b98b0ee7596; "
              "ttwid=1%7CTOw3V-1qPgQfcCT4S0Hhey26Q4PJq9J6nGaLq6GMe14%7C1651580656"
              "%7C48653bc0c5afd31f1a919f9bd17a80173a1ea69d96b23359b103c4f078ef969b; support_webp=true; "
              "support_avif=true; _tea_utm_cache_1300=undefined",
    "Referer": "https://www.ixigua.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/86.0.4240.75 Safari/537.36"
}
 
 
def downloadFile(url, name):
    print(f'开始下载: {name}')
    resp = requests.get(url, headers=headers)
    if not os.path.exists('movie'):
        os.mkdir('movie')
    with open(f'movie/{name}', mode='wb') as f:
        f.write(resp.content)
    print(f'完成下载: {name}')
 
 
def downloadAll(video_url, audio_url, title):
    if not os.path.exists('movie'):
        os.mkdir('movie')
    if not os.path.exists(f'movie/{title}-merger.mp4'):
        downloadFile(video_url, title + '.mp4')
        downloadFile(audio_url, title + '.mp3')
        # 使用ffmpeg进行音频视频合并 替换自己电脑ffmpeg的安装地址
        # D:/JetBrains/ffmpeg-5.0.1/ffmpeg-5.0.1-essentials_build/bin/ffmpeg
        command = f'D:/JetBrains/ffmpeg-5.0.1/ffmpeg-5.0.1-essentials_build/bin/ffmpeg -i movie/{title}.mp4 -i movie/{title}.mp3 -c:v copy -c:a aac -strict experimental movie/{title}-merger.mp4'
        print('开始合并音频视频......')
        subprocess.run(command, shell=True)
        os.remove(f'movie/{title}.mp4')
        os.remove(f'movie/{title}.mp3')
        print(f'完成合并,见目录: movie/{title}-merger.mp4')
 
 
def main(url):
    resp = requests.get(url=url, headers=headers)
    resp.encoding = 'utf-8'
    if resp.status_code == 200:
        res_html = resp.text
        if 'pseries_more_v2' not in url:
            json_str = re.findall('window._SSR_HYDRATED_DATA=(.*?)</script>', res_html)[0]
            json_str = json_str.replace('undefined', 'null')
            json_data = json.loads(json_str)
            video_url = \
                json_data['anyVideo']['gidInformation']['packerData']['video']['videoResource']['dash'][
                    'dynamic_video'][
                    'dynamic_video_list'][-1]['main_url']
            audio_url = \
                json_data['anyVideo']['gidInformation']['packerData']['video']['videoResource']['dash'][
                    'dynamic_video'][
                    'dynamic_audio_list'][-1]['main_url']
            title = json_data['anyVideo']['gidInformation']['packerData']['video']['title']
            title = re.sub(r"[\/\\\:\*\?\"\<\>\|]", "_", title).replace(" ", "")
            video_url = base64.b64decode(video_url).decode()
            audio_url = base64.b64decode(audio_url).decode()
            downloadAll(video_url, audio_url, title)
        else:
            json_data = json.loads(res_html)
            for item in json_data['data']:
                title = item['title']
                # backup_url_1
                video_url = item['preloadVideoResource']['dynamic_video']['dynamic_video_list'][-1][
                    'backup_url_1']
                audio_url = item['preloadVideoResource']['dynamic_video']['dynamic_audio_list'][-1][
                    'backup_url_1']
                video_url = base64.b64decode(video_url).decode()
                audio_url = base64.b64decode(audio_url).decode()
                title = re.sub(r"[\/\\\:\*\?\"\<\>\|]", "_", title).replace(" ", "")
                downloadAll(video_url, audio_url, title)
 
 
if __name__ == '__main__':
    # 单个视频 https://www.ixigua.com/6895255008762135051
    # 视频合集 https://www.ixigua.com/api/videov2/pseries_more_v2?pSeriesId=7069375938756411911&rank=0&tailCount=30
    # 关于下载视频合集的几个参数 rank:每页的起始数字  tailCount: 一页展示多少个
    # 比如第一页: rank=0&tailCount=30 第二页: rank=30&tailCount=30 第三页: rank=60&tailCount=30
    # 直接复制地址栏中长数字(类似:6895255008762135051)替换到链接中即可
 
    # 单个视频下载
    # 原链接:https://www.ixigua.com/6989911204554605064
    main('https://www.ixigua.com/6989911204554605064')
 
    # 合集视频下载
    # 原链接: https://www.ixigua.com/6888602680952881676?id=6887944298654859780
    # 将6888602680952881676 替换到下面链接中
    main('https://www.ixigua.com/api/videov2/pseries_more_v2?pSeriesId=6888602680952881676&rank=0&tailCount=30')