爬虫爬m3u8视频

复制代码
#知识点
#理解m3u8视频结构  把长的视频切成小片段,不断加载的过程
#Requests模块使用
#json数据提取
#re模块使用
#bs4提取数据
#tqdm模块使用
import json
from bs4 import BeautifulSoup
import pprint
import re
from tqdm import tqdm
import requests

headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
# 'Cookie':'csrfToken=1iGcGyCmLIJQNJCq7aUp2Vrn; _did=web_9682077584854F37; webp_supported=%7B%22lossy%22%3Atrue%2C%22lossless%22%3Atrue%2C%22alpha%22%3Atrue%2C%22animation%22%3Atrue%7D; Hm_lvt_2af69bc2b378fb58ae04ed2a04257ed1=1721353272; HMACCOUNT=ADC202C257290F1A; lsv_js_player_v2_main=e4d400; uuid=7ae76485943e1f1af48d666de88ae802; safety_id=AAFo3UJctQI2vE8rhmVPB5vN; _did=web_9682077584854F37; Hm_lpvt_2af69bc2b378fb58ae04ed2a04257ed1=1721355283; cur_req_id=568175026C6CA7DD_cde007fae07665f9c236fc1842944b25_video_monkey_recommend; cur_group_id=568175026C6CA7DD_cde007fae07665f9c236fc1842944b25_video_monkey_recommend_1',
# 'Referer':'https://www.acfun.cn/'
}
#获取m3u8列表文件
def get_m3u8_list(url):
    r=requests.get(url,headers=headers)
    # print(r.status_code)
    # print(r.text)
    info=re.findall(' window.pageInfo = window.videoInfo =(.*?) window.videoResource',r.text,re.S)[0].strip()[:-1]#re.S允许换行
    # print(info)
    info_json=json.loads(json.loads(info)['currentVideoInfo']['ksPlayJson'])['adaptationSet'][0]['representation'][0]['url']#转换成json
    filename=json.loads(info)['title']
    filename=re.sub(r'[|><>/\\]','',filename)#是一个用于替换字符串中匹配的部分的正则表达式函数r'[|><>/\\]' 是正则表达式模式,表示要匹配的字符集合。具体来说:/ 和 \ 表示斜杠和反斜杠(\\ 在正则表达式中表示反斜杠字符)。'' 是替换的内容,即将匹配到的字符替换为空字符(即删除这些字符)。filename 是要处理的字符串变量。
    print(filename)
    # pprint.pprint(info_json)
    return info_json,filename
#提取所有视频片段的播放地址 ts文件
def get_ts_files(url):
    r=requests.get(url,headers=headers)
    ts_files=re.sub('#.*','',r.text).split()
    #print(ts_files)
    return ts_files
#下载并合并视频片段
def download_combine(ts_files,path,filename):
    with open(f'{path}/{filename}.mp4','ab') as f:
        for ts in tqdm(ts_files):
            ts='https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/'+ts
            ts_content=requests.get(ts,headers=headers).content
            #print(ts_content)
            f.write(ts_content)


#获取目录页的视频链接
def get_index_links(index_url):
    r=requests.get(index_url,headers=headers)
    soup=BeautifulSoup(r.text,'html.parser')
    link_list=soup.find_all('h1',class_='list-content-title')
    links=[]
    for a in link_list:
        link="https://www.acfun.cn"+a.a.get('href')#a.a 表示从 a 对象中获取其嵌套的第一个 <a> 标签(即超链接标签)。
        links.append(link)
    return links
def main():
    #index_url="https://www.acfun.cn/v/list135/index.htm?sortFiled=rankScore&duration=all&default&page=1";
    index_url = "https://www.acfun.cn/v/list135/index.htm"
    links=get_index_links(index_url)
    path = 'D:\python学习笔记课后题\爬虫\实战\视频'
    for url in links:
        #url='https://www.acfun.cn/v/ac34857244'
        m3u8_url,filename=get_m3u8_list(url)
        ts_files=get_ts_files(m3u8_url)
        download_combine(ts_files,path,filename)

if __name__ == '__main__':
    main()
相关推荐
EasyGBS17 小时前
国标GB28181视频平台EasyGBS即将重磅新增WHIP推流功能!低延迟直播体验再升级
音视频
jiejiejiejie_18 小时前
Flutter for OpenHarmony 萌系实战合集:地图功能 + 音频播放一站式指南
flutter·音视频
jbk331119 小时前
10分钟翻译一条视频,实现语音、字幕翻译后与画面同步对齐,视频翻译助手使用教程
人工智能·音视频·剪辑软件·剪映自动化软件
带娃的IT创业者19 小时前
DaVinci Resolve – Photo:当视频调色之王,跨界“修图”,意味着什么?
图像处理·音视频·工作流·davinci resolve·后期制作·视频调色·色彩管理
nashane1 天前
HarmonyOS Video组件预览图片优化实践:告别黑屏,提升视频播放体验
华为·音视频·harmonyos·harmonyos 5
科研前沿1 天前
2026 数字孪生前沿科技:全景迭代报告 —— 镜像视界生成式孪生(Generative DT)技术白皮书
大数据·人工智能·科技·算法·音视频·空间计算
EasyDSS1 天前
私有化视频会议系统/视频高清直播点播EasyDSS一体化视频平台赋能各行业数字化高效协同
音视频
科研前沿1 天前
镜像视界浙江科技有限公司的关键技术突破有哪些?
大数据·人工智能·科技·算法·音视频·空间计算
Python大数据分析@1 天前
CLI一键采集,使用Python搭建TikTok电商爬虫Agent
开发语言·爬虫·python
编程隐士1 天前
爬虫管理系统实现方案
爬虫