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