python爬取m3u8视频

1.获取m3u8列表文件

def get_m3u8_list(url):
    r=requests.get(url,headers=headers)
    info=re.findall(' window.pageInfo = window.videoInfo =(.*?) window.videoResource',r.text,re.S)[0].strip()[:-1]
    info_json=json.loads(json.loads(info)['currentVideoInfo']['ksPlayJson'])['adaptationSet'][0]['representation'][0]['url']
    filename=json.loads(info)['title']
    print(filename)
    return info_json,filename

2.提取所有视频片段的播放地址 ts文件

def get_ts_files(url):
    r=requests.get(url,headers=headers)
    ts_files=re.sub('#.*','',r.text).split()
    return ts_files

3.下载并合并视频片段

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
            f.write(ts_content)

4.获取目录页的视频链接

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')
        links.append(link)
    return links

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值