使用Python下载B站视频片段-curl命令转Python代码

本文将使用Python代码实现B站视频片段的下载,并解决常见的SSL错误。

1. 原curl命令:

curl 'https://hknm5s6gzvm5a6wju24.exp.bcevod.com/mda-if4e9x4zg2n8kfce/mda-if4e9x4zg2n8kfce.m3u8.0.ts' \
  -H 'authority: hknm5s6gzvm5a6wju24.exp.bcevod.com' \
  -H 'accept: */*' \
  -H 'accept-language: zh-CN,zh;q=0.9,en;q=0.8' \
  -H 'if-modified-since: Tue, 05 Jun 2018 02:24:10 GMT' \
  -H 'if-none-match: '11bfb8e05c12a0206d7f7504a92645af'' \
  -H 'origin: https://1s1k.eduyun.cn' \
  -H 'referer: https://1s1k.eduyun.cn/portal/redesign/index/index.jsp?t=2&sdResIdCaseId=8aee800063c9a0880163ca23847d03a5&sessionKey=jigyYugr03brcoSngbvb' \
  -H 'sec-ch-ua: 'Chromium';v='110', 'Not A(Brand';v='24', 'Google Chrome';v='110'' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: 'Windows'' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: cross-site' \
  -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36' \
  --compressed

2. Python代码:

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
import ssl

url = 'https://hknm5s6gzvm5a6wju24.exp.bcevod.com/mda-if4e9x4zg2n8kfce/mda-if4e9x4zg2n8kfce.m3u8.0.ts'
headers = {
    'authority': 'hknm5s6gzvm5a6wju24.exp.bcevod.com',
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'if-modified-since': 'Tue, 05 Jun 2018 02:24:10 GMT',
    'if-none-match': '11bfb8e05c12a0206d7f7504a92645af',
    'origin': 'https://1s1k.eduyun.cn',
    'referer': 'https://1s1k.eduyun.cn/portal/redesign/index/index.jsp?t=2&sdResIdCaseId=8aee800063c9a0880163ca23847d03a5&sessionKey=jigyYugr03brcoSngbvb',
    'sec-ch-ua': "'Chromium';v='110', 'Not A(Brand';v='24', 'Google Chrome';v='110'",
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': "'Windows'",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'cross-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
}

# 关闭证书验证
context = create_urllib3_context()
context.verify_mode = ssl.CERT_NONE

# 使用session保持连接,减少连接开销
session = requests.Session()
session.mount('https://', HTTPAdapter(max_retries=5))

response = session.get(url, headers=headers, verify=False)
with open('video.ts', 'wb') as f:
    f.write(response.content)

3. SSL错误解决方案:

如果遇到SSL错误,可以尝试加入以下参数来解决:

  • verify=False:关闭SSL证书验证

4. 代码解释:

  • requests 库用于发送HTTP请求
  • HTTPAdapter 用于配置连接池,提高下载速度
  • create_urllib3_context 用于创建SSL上下文,并关闭证书验证
  • session 用于保持连接,减少连接开销
  • headers 字典保存了请求头信息,与curl命令中的-H参数对应
  • response.content 获取下载内容
  • with open('video.ts', 'wb') as f: 使用上下文管理器打开文件,并以二进制写入模式写入下载内容

5. 注意:

  • 由于关闭了SSL证书验证,建议只在可信环境下使用
  • 下载的视频片段可能需要与其他片段合并才能完整播放

6. 总结:

本文介绍了如何将curl命令转换成Python代码,并解决了常见的SSL错误。通过使用requests库和SSL证书验证配置,可以方便地下载B站视频片段。

使用Python下载B站视频片段-curl命令转Python代码

原文地址: https://www.cveoy.top/t/topic/m5u4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录