爬取m3u8视频

网址:https://www.bhlsm.com/cupfoxplay/609-3-1/

相关代码:

python 复制代码
#采集网址:https://www.bhlsm.com/cupfoxplay/609-3-1/
#正常视频网站:完整视频内容
# pip install pycryptodomex
#流媒体文件:M3U8(把完整的视频内容,分割成N个视频片段,ts文件)
"""
第一次请求:获取m3u8文件链接  / 视频标题
    1.发送请求:
        - 请求网址:视频播放页面链接
    2.获取数据:
        - 服务器返回响应数据
    3.解析数据:
        - 提取么u3u8文件链接 / 视频标题
AES-128:
    1. key:密钥 enc.key https://v.gsuus.com/play/QBY0yWKa/enc.key
"""
import requests
#导入正则表达式模块
import re
#导入加密模块
from Cryptodome.Cipher import AES
#模拟浏览器
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
    }
#请求网址
for page in range(1,4):
    url = f'https://www.bhlsm.com/cupfoxplay/609-3-{page}/'
    #发送请求
    response = requests.get(url=url,headers=headers)
    # print(response)
    html_data = response.text
    #解析数据,提取视频标题 -> re.findall('匹配数据','数据源')
    # re.findall('<h1 class="movie-list-title">(斗罗大陆1 第01集)在线播放</h1>')
    title = re.findall('<h1 class="movie-list-title">(.*?)在线播放</h1>',html_data)[0]
    #提取m3u8链接
    m3u8_url = re.findall('"url":"(.*?)","url_next":',html_data)[0].replace('\/','/')

    print(title,m3u8_url)

    #获取密钥(每一集的密钥不相同)
    # key_url = 'https://v.gsuus.com/play/QBY0yWKa/enc.key'
    key_url = f'https://v.gsuus.com/play/{m3u8_url.split("/")[-2]}/enc.key'
    key = requests.get(url=key_url,headers=headers).content
    #解码器
    ci = AES.new(key,AES.MODE_CBC)
    # print(html_data)

    # print(title)
    """
    第二次请求:获取所有ts文件链接
    1.发送请求:
        - 请求网址:m3u8文件链接
    2.获取数据:
        - 服务器返回响应数据
    3.解析数据:
        - 提取  ts文件链接(230个视频片段)
    """
    #发送请求 + 获取数据
    m3u8_data = requests.get(url=m3u8_url,headers=headers).text
    #解析数据,提取ts链接
    ts_list = re.findall(',\n(.*?)\n#',m3u8_data)


    """
    ,
    https://gs.gszyi.com:999/hls/46/20230223/1034032/plist-00001.ts
    #
    """
    # print(m3u8_data)
    #for循环遍历,提取列表里面元素

    # print(ts_list)

    for ts in ts_list:
        """
        第三次请求:获取视频内容
            1.发送请求:
                - 请求网址:ts文件链接
            2.获取数据:
                - 获取视频内容
            3.保存数据:
                - 把所有视频派那段保存成一个完整的视频内容
        
        """
        # print(ts)
        #发送请求ts链接+ 获取视频数据
        ts_content = requests.get(url=ts,headers=headers).content
        #进行解码
        content = ci.decrypt(ts_content)
        with open('video\\' + title + '.mp4',mode='ab') as f:
            f.write(content)
        print(ts)
        #break

爬取过程:

查找url:

相关推荐
人工智能研究所30 分钟前
使用OpenCV与Python编写自己的俄罗斯方块小游戏
开发语言·python·opencv
DDD小小小宇宙31 分钟前
python列表基础知识
开发语言·windows·python
@黄色海岸1 小时前
【sklearn 05】sklearn功能模块
人工智能·python·sklearn
追逐☞1 小时前
PyTorch使用-张量类型转换
人工智能·pytorch·python
懒大王爱吃狼1 小时前
Python + Qt Designer构建多界面GUI应用程序:Python如何调用多个界面文件
开发语言·数据库·python·qt·mysql·python基础·命令模式
北京_宏哥1 小时前
🔥《手把手教你》系列练习篇之8-python+ selenium自动化测试(详细教程)
前端·python·selenium
北京_宏哥2 小时前
🔥《手把手教你》系列练习篇之7-python+ selenium自动化测试(详细教程)
前端·python·selenium
三道杠卷胡2 小时前
【AI News | 20250316】每日AI进展
人工智能·python·语言模型·github·aigc
这里有鱼汤2 小时前
Python编程新境界:掌握函数式编程,让你的代码优雅到飞起
后端·python
这里有鱼汤2 小时前
如何让 Python 代码像口红一样有质感?答案是 dataclass!
后端·python