第一步分析:
现在虎牙视频基本都是m3u8视频格式
第二步代码实现:
爬取一个
# @Author: 昊阳
# _*_ coding : utf-8 _*_
# @Time:2022/10/22 0022 10:28
# @File : TV_m3u8
# @Project : MyCase
# 视频内容完整地址连接
# https://liveapi.huya.com/moment/getMomentContent?callback=jQuery112404941461534257865_1666406829965&videoId=671081861&uid=&_=1666406829971
# 但是我们只需要视频信息里面的有关json数据
#https://liveapi.huya.com/moment/getMomentContent?videoId=671081861&uid=&_=1666406829971
import requests
import pprint #格式化输出
url = "https://liveapi.huya.com/moment/getMomentContent?videoId=671081861&uid=&_=1666406829971"
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
response = requests.get(url=url,headers=headers).json()
# 格式化输出
# pprint.pprint(response)
#获取的是一个json字典数据,根据键值对取值
# 视频名
title = response['data']['moment']['title']
# 视频地址
video_url = response['data']['moment']['videoInfo']['definitions'][0]['url']
# print(title)
# print(video_url)
# 保存视频
video_content = requests.get(url=video_url,headers=headers).content
with open('video\\'+title+'.mp4',mode='wb') as f:
f.write(video_content)
print(title,'下载完成')