import requests
import re
import os
#要下载的页面url
url="https://hsex.men/video-831510.htm"
#要保存的目录
download_dir="F:/HuLuWa"
html=requests.get(url).text
re_url=r"\b(?:https?://\S+?index\.m3u8\S*)\b"
re_title= r"<title>(.*?)</title>"
re_ts= r"\bindex\w*\.ts\b"
title=re.findall(r"[\u4e00-\u9fa5a-zA-Z0-9]+",re.findall(re_title,html)[0])
title="".join(title)
urls=re.findall(re_url,html)
#获取m3u8
m3u8=requests.get(urls[0]).text
#获取ts文件表
tsList=re.findall(re_ts,m3u8)
#开始以二进制下载ts文件
video=b''
for item in tsList:
tmp_url=urls[0].replace("index.m3u8",item)
tmpbytes=requests.get(tmp_url).content
video+=tmpbytes
print('下载成功:',item)
save_path=os.path.sep.join([download_dir,title+".ts"])
with open(save_path,"wb+") as f:
f.write(video)
f.flush()
f.close()
print("保存成功:",save_path)
可以直接下载的脚本,下载格式为ts,如果需要自行转码