爬取某牙视频

爬取页面链接:游戏视频_游戏攻略_虎牙视频

爬取步骤:点进去一个视频播放,查看media看有没有视频,发现没有。在xhr中发现有许多ts文件,但这种不是很长的视频一般都有直接的播放链接,所以目标还是找直接的链接。在搜索中搜索ts文件的某一个参数,或直接搜m3u8可以找到getmonment的包,里面有下载的链接。而这个包的链接与视频id有关

一页如何下载:在主界面找到含有多个视频id的包,爬取视频id和视频名称,再循环将id赋值给getmonment的包的链接,实现一页下载。

多页下载:观察主界面的包找url的规律即可。

代码展现:

python 复制代码
import requests
import re
import os
from tqdm import tqdm
filename = 'video虎牙\\'
if not os.path.exists(filename):
    os.mkdir(filename)
url = 'https://www.huya.com/video/g/all?set_id=37&order=hot&page=1'
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}

response = requests.get(url=url,headers=headers).text

id_list = re.findall('\{"vid":(.*?),',response)


for id in tqdm(id_list):
    headers1 = {
        "Referer":"https://www.huya.com/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"

    }
    link = f'https://liveapi.huya.com/moment/getMomentContent?videoId={id}&uid=&_=1708997648767'
    json_data = requests.get(url=link,headers=headers).json()
    video_name = json_data['data']['moment']['title']
    video_url = json_data['data']['moment']['videoInfo']['definitions'][0]['url']
    print(f'正在下载:{video_name}')
    video_content = requests.get(url=video_url,headers=headers1).content
    with open(filename+video_name+'.mp4','wb') as f:
        f.write(video_content)

结果展现:

相关推荐
CryptoPP36 分钟前
使用WebSocket实时获取印度股票数据源(无调用次数限制)实战
后端·python·websocket·网络协议·区块链
树叶@37 分钟前
Python数据分析7
开发语言·python
老胖闲聊2 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
码界奇点2 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
浠寒AI2 小时前
智能体模式篇(上)- 深入 ReAct:LangGraph构建能自主思考与行动的 AI
人工智能·python
行云流水剑3 小时前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
心扬4 小时前
python生成器
开发语言·python
mouseliu4 小时前
python之二:docker部署项目
前端·python
狂小虎4 小时前
亲测解决self.transform is not exist
python·深度学习
Python智慧行囊4 小时前
Python 中 Django 中间件:原理、方法与实战应用
python·中间件·架构·django·开发