爬取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:

相关推荐
孟健4 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞6 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽8 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程13 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪13 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook13 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 天前
Pydantic配置管理最佳实践(一)
python