前言:随着技术的不断革新,现在很多站点大站都是无法直接抓取到直播源的,就比如“虎牙直播”,去年(2020年)还能获取到虎牙永久直播源,今年很多规则都已经失效,所以今天准备给大家出一个虎牙直播的抓取教程,本教程相对网络其他教程已经算是很简单了,请仔细阅读,你也可以轻松获取。
一:准备工具
1.谷歌浏览器
2.科学上网环境
3.黑鸟播放器或PotPlayer (黑鸟播放器:点击下载)
4.虎牙直播网页
二:抓取教程
1.首先确保电脑已处于科学上网环境,打开谷歌浏览器在地址栏输入下面地址,打开chrome 网上应用店
https://chrome.google.com/webstore/category/extensions?hl=zh-CN
2.按下图提示搜索“tampermonkey”;点击第一个进入后点击“添加至 Chrome”等待安装完成
3.安装完成后在地址栏输入,打开扩展
chrome://extensions/
4.打开后,开启开发模式,启用刚下下载的扩展
5.按下图将扩展程序添加置顶到浏览器主页
6.按下图点击添加脚本
7.脚本代码如下,复制到其中粘贴,并保存
// ==UserScript==
// @name 获取虎牙直播流地址,可直接使用VLC播放器播放
// @namespace http://tampermonkey.net/
// @version 0.6
// @description 获取虎牙直播流地址,可直接使用VLC播放器播放,在VLC内选择Open Network,粘贴地址打开即可
// @author xiaozhuai
// @include http://www.huya.com/*
// @include https://www.huya.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function heredoc(fn) {
return fn.toString().split('\n').slice(1,-1).join('\n') + '\n'
}
var boxHtml = '';
if(window.TT_ROOM_DATA.state==='OFF'){
boxHtml = '状态: 未开播<br>';
}
if(window.TT_ROOM_DATA.state==='ON'){
boxHtml = '状态: 正在直播<br>';
try{
var streamInfoList = window.hyPlayerConfig.stream.data[0].gameStreamInfoList;
var data = [];
for(var line=0; line<streamInfoList.length; line++){
var streamInfo = streamInfoList[line];
console.log(streamInfo);
var lineNumber = streamInfo.iLineIndex;
var url = streamInfo.sFlvUrl + '/' + streamInfo.sStreamName + '.' + streamInfo.sFlvUrlSuffix + '?' + streamInfo.sFlvAntiCode;
var ratioList = window.hyPlayerConfig.stream.vMultiStreamInfo;
for(var i=0; i<ratioList.length; i++){
var label = ratioList[i].sDisplayName;
var absUrl = url;
if(ratioList[i].iBitRate != 0){
absUrl = url+"&ratio="+ratioList[i].iBitRate;
}
data.push({
label: '(线路'+lineNumber+') '+label,
url: absUrl,
});
}
}
for(var i=0; i<data.length; i++) {
boxHtml += '<div class="flv-url-item"><label>'+data[i].label+'</label><input id="flv-url-'+i+'" value="'+data[i].url+'"/><a onclick="copyFlvUrl('+i+')">复制</a></div>'; //<a onclick="openFlvUrl('+i+')">VLC播放</a>
}
}catch(e){
boxHtml += '解析流数据错误';
console.error(e);
}
}
if(window.TT_ROOM_DATA.state==='REPLAY'){
boxHtml = '状态: 重播<br>';
boxHtml += '当前处于重播状态,不能解析流数据';
}
window.toggleFlvUrlBox = function() {
var flvUrlBoxBtn = document.getElementById('flv-url-box-btn');
var flvUrlBox = document.getElementById('flv-url-box');
if(flvUrlBox.style.display==='none'){
flvUrlBox.style.display='block';
}else{
flvUrlBox.style.display='none';
}
}
window.copyFlvUrl = function(index) {
var input = document.getElementById('flv-url-'+index);
input.select();
document.execCommand("Copy");
}
window.openFlvUrl = function(index) {
var input = document.getElementById('flv-url-'+index);
var url = input.value;
location.href = "vlc://"+url
}
var wrapper = document.createElement("div");
wrapper.style.display = 'inline-block';
wrapper.innerHTML = heredoc(function(){/*
<style>
#flv-url-box-btn {
width: 32px;
height: 32px;
cursor: pointer;
background-color: #ffffff;
top: 9px;
right: 40px;
position: fixed;
z-index: 1000000;
border-radius: 4px;
border: 1px solid #cccccc;
}
#flv-url-box-btn:hover{
box-shadow: 0 0 8px #0ca4d4;
}
#flv-url-box {
top: 54px;
right: 40px;
border: 1px solid #808080;
border-radius: 6px;
background-color: #ffffff;
padding: 8px;
position: fixed;
z-index: 1000000;
}
#flv-url-box .flv-url-item{
margin: 4px 0;
}
#flv-url-box .flv-url-item>*{
border: 1px solid #808080;
margin-left: -1px;
vertical-align: top;
}
#flv-url-box .flv-url-item>*:first-child{
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
margin-left: 0;
}
#flv-url-box .flv-url-item>*:last-child{
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
}
#flv-url-box .flv-url-item input{
top: 0;
width: 240px;
height: 24px;
padding: 0 4px;
}
#flv-url-box .flv-url-item input:focus{
outline: none;
border-color: #0ca4d4;
}
#flv-url-box .flv-url-item a{
user-select: none;
padding: 0 4px;
cursor: pointer;
font-size: 12px;
line-height: 24px;
height: 24px;
display: inline-block;
background-color: #ffffff;
color: #333;
}
#flv-url-box .flv-url-item label{
user-select: none;
text-align: left;
padding-left: 12px;
width: 100px;
font-size: 12px;
line-height: 24px;
height: 24px;
display: inline-block;
background-color: #ffffff;
color: #333;
}
#flv-url-box .flv-url-item a:hover{
border-color: #0ca4d4;
color: #0ca4d4;
}
</style>
<div>
<img id="flv-url-box-btn" onclick="toggleFlvUrlBox()" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADYAAAAvCAYAAAConDmOAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAAB4AAAAeACd9VpgAAAAB3RJTUUH4QMTFwIhc/YnDwAABeNJREFUaN7d2k9oG9kBx/Hvm5G8sizFju38UeyuccCytcTktElocygte+mt0G6bbmkhkIRAriXgQA4LS28l0FMOxSo5BJcUcig0hxKKnCiiwoGkru21JNt1TEJQJVm2ZM94Zt7bgz3aKI6TOB5b3v7AYI1mnt/nvZn3Zt5Y8Ib86MefYZoG3d0f4/P58CpSSiYm/k15cREhhGflzs7ObtpWV+uff34O27IIBAIEmpupVitNgO5VBUKhEH5/k1fFOcAaQG9v7yZgrdl+8csvAPjoowBra+YnQojPhRCfCiFCgNppLYQQLC6WmP56EsuydlwcUAXSwF+A/7hfuDgB8MWvf4vjOKysVLWWltCvhBBfCiF6vWpaIQSVyjK5bIZKZdmrYt3MA18Bf2K9F5mdnV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ok1pbWV0eXBlAGltYWdlL3BuZz+yVk4AAAAXdEVYdFRodW1iOjpNVGltZQAxNDg5OTM1NzUz3MRh0wAAABJ0RVh0VGh1bWI6OlNpemUAMjU4NzdCDe2HSgAAAF90RVh0VGh1bWI6OlVSSQBmaWxlOi8vL2hvbWUvd3d3cm9vdC9zaXRlL3d3dy5lYXN5aWNvbi5uZXQvY2RuLWltZy5lYXN5aWNvbi5jbi9zcmMvMTIwNzcvMTIwNzcxMS5wbmcp913KAAAAAElFTkSuQmCC" />
<div id="flv-url-box" style="display: none;">__box_html__</div>
<div>
*/}).replace('__box_html__', boxHtml);
document.body.append(wrapper);
})();
8.点击旁边的“已安装脚本”打开启用
9.打开虎牙直播网站的任意直播页面,如“https://www.huya.com/24447241”,点击右上角的小电视图标即可查看到所有直播源地址
10.随便复制一个地址,打开上述工具“黑鸟播放器”,右键“打开地址”,并粘贴
11.至此虎牙直播源就抓取到了。
三:其他可选
1.黑鸟播放器也可以直接获取虎牙的直播源地址并播放,右键“片库搜索”
2.没有账号自己注册一个,按下图选择或粘贴直播间地址解析
3.按下图即可解析出虎牙的真实直播地址,不过需要注意的是M3U8地址,虎牙更新规则后无法播放,所以我们选择FLV
4.选择一个地址右键复制,在播放器主页右键打开链接即可播放
延伸阅读:
1.如需要其他操作请参考《虎牙直播PHP代理源码+API调用接口》
2.如需PHP直播管理网站系统,请下载《电视直播管理系统源码》
3.如需习惯使用python,请以使用Python代码《获取直播平台真实流媒体地址》