采集某易云的PHP代码备份一下
时间:2023年10月04日
/来源:网络
/编辑:佚名
做了测试,采集了一下某易云的歌曲,大概采集了两万多歌手和一千万歌曲,当然了,数据我都删除了,主要是为了写代码研究一下,代码这里备份一下
<?php
header('Content-Type: text/html; charset=utf-8');
class NeteaseMusicAPI
{
private $ENCRYPT_N = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';
private $ENCRYPT_NONCE = '0CoJUm6Qyw8W8jud';
private $ENCRYPT_E='010001';
private $AES_VI='0102030405060708';
protected $_USERAGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.157 Safari/537.36';
protected $_REFERER='http://music.163.com/';
// key
protected $secretKey='TA3YiYCfY2dDJQgg';
protected $encSecKey='84ca47bca10bad09a6b04c5c927ef077d9b9f1e37098aa3eac6ea70eb59df0aa28b691b7e75e4f1f9831754919ea784c8f74fbfadf2898b0be17849fd656060162857830e241aba44991601f137624094c114ea8d17bce815b0cd4e5b8e2fbaba978c6d1d14dc3d1faf852bdd28818031ccdaaa13a6018e1024e2aae98844210';
private function prepare($raw)
{
$data['params'] =$this->aes_encode(json_encode($raw), $this->ENCRYPT_NONCE);
$data['params'] = $this->aes_encode($data['params'], $this->secretKey);
$data['encSecKey'] = $this->encSecKey;
return $data;
}
private function aes_encode($secretData, $secret)
{
return openssl_encrypt($secretData, 'aes-128-cbc', $secret, false, $this->AES_VI);
}
/**
* CURL 模块
* @param string $uri 目的地址
* @param string $postData POST数组
* @param string $cookie 携带Cookie
* @param string|array $header 自定义Header
* @return string
*/
protected function http_requests($uri, $postData = '', $cookie = '', $header = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($postData) { // post提交
if (is_array($postData)) $postData = http_build_query($postData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
if ($cookie) // 伪造cookie
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
if ($header) // 自定义header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public function url($song_id, $br = 999000){
$url = 'http://music.163.com/weapi/song/enhance/player/url?csrf_token=';
if (!is_array($song_id)) $song_id = [$song_id];
$data = [
'ids' => $song_id,
'br' => $br,
'csrf_token' => '',
];
return $this->http_requests(
$url,
$this->prepare($data),
'os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true',
[
'Origin: http://music.163.com',
'X-Real-IP: 183.30.197.115',
'Accept-Language: q=0.8,zh-CN;q=0.6,zh;q=0.2',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Referer: http://music.163.com/'
]
);
}
}
$a = new NeteaseMusicAPI();
$result = $a->url('18614850', 96000);
$array = json_decode($result, true);
var_dump($array);
?>
输出结果(php7 测试通过):
根据歌手id采集歌曲:
歌手ID大全:
两万多个,我就放我百度云吧,不然网页卡成狗
163.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>网易云音乐外链</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link href="./163music.css" rel="stylesheet">
<script src="//cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("script").last().remove();
$("div").last().remove();
var id = window.setTimeout(function () {
}, 0);
while (id--) {
window.clearTimeout(id);
}
});
</script>
<style type="text/css" media="screen" id="test">
</style>
</head>
<body>
<h2>查询结果如下</h2>
<div id="back"><a href="index.html">返回搜索</a></div>
<p>如果播放选项是灰色就表示这首歌是VIP专享</p>
<?php
session_start();//尝试通过session传递歌词参数
require 'Meting.php';
use Metowolf\Meting;
$api = new Meting('netease');
//header("Content-type:text/html;charset=utf-8");
function curl_get($url)
{
$refer = "http://music.163.com/";
$header[] = "Cookie: " . "appver=1.5.0.75771;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $refer);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$url = "http://music.163.com/api/search/get/web?csrf_token=";
$s = $_POST["search"];
$limit = 15;
function curl($url, $s, $limit)
{
$curl = curl_init();
$post_data = 'hlpretag=<span class="s-fc7">&hlposttag=</span>&s=' . $s . '&type=1&offset=0&total=true&limit=' . $limit;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$header = array(
'Host: music.163.com',
'Origin: http://music.163.com',
'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded',
'Referer: http://music.163.com/search/',
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$src = curl_exec($curl);
curl_close($curl);
return $src;
}
function netease_encryptId($id)
{
$magic = str_split('3go8&$8*3*3h0k(2)2');
$song_id = str_split($id);
for ($i = 0; $i < count($song_id); $i++) {
$song_id[$i] = chr(ord($song_id[$i]) ^ ord($magic[$i % count($magic)]));
}
$result = base64_encode(md5(implode('', $song_id), 1));
$result = str_replace(array('/', '+'), array('_', '-'), $result);
return $result;
}
//判断是否输入了歌曲名
if (!$s || !$limit) {
$tempArr = array("code" => -1, "msg" => "请输入歌曲名");
echo "<h2 style='color:yellow;text-align:center;'>" . "soory!" . $tempArr["msg"] . "!特么不输入歌曲名,怎么搜啊!!!" . "</h2>";
} else {
$ID_array = json_decode(curl($url, $s, $limit), true);//将json转换数组
echo "<table border='2'>";
echo "<tr>";
echo "<td>" . "歌曲序号" . "</td>";
echo "<td>" . "歌曲ID" . "</td>";
echo "<td>" . "歌曲名称" . "</td>";
echo "<td>" . "歌手" . "</td>";
echo "<td>" . "歌曲链接:" . "</td>";
echo "<td>" . "专辑封面:" . "</td>";
echo "<td>" . "专辑名:" . "</td>";
echo "<td>" . "歌词:" . "</td>";
echo "</tr>";
for ($i = 0; $i < $limit; $i++) {
$datas = $api->format(true)->song($ID_array["result"]["songs"][$i]["id"]);
$datas = json_decode($datas, true);
$url = 'https://p3.music.126.net/' . netease_encryptId($datas[0]['pic_id']) . '/' . $datas[0]['pic_id'] . '.jpg?param=200y200';
$data = $datas[0];
$lrc = $api->lyric($data['id']);
$lrc = json_decode($lrc, true);
echo "<tr>";
echo "<td>" . ($i + 1) . "</td>";
echo "<td>" . $ID_array["result"]["songs"][$i]["id"] . "</td>";
echo "<td>《" . $ID_array["result"]["songs"][$i]["name"] . "》</td>";//输出歌曲名
echo "<td>" . $ID_array["result"]["songs"][$i]["artists"][0]["name"] . "</td>";//输出歌手名
echo "<td> <audio src='http://music.163.com/song/media/outer/url?id={$ID_array["result"]["songs"][$i]["id"]}.mp3' controls='controls'></audio></td>";//通过歌曲id拼接出MP3歌曲链接
echo "<td><img src='$url' alt=''></td>";//专辑封面
echo "<td>{$datas[0]["album"]}</td>";//专辑名
$_SESSION["lyric" . $ID_array["result"]["songs"][$i]["id"]] = $lrc;
echo "<td><a href='lyric.php?num={$ID_array["result"]["songs"][$i]["id"]}' target='_blank'>查看歌词</a></td>";//歌词页
echo "</tr>";
}
echo "</table>";
}
?>
<div class="achor">
<ul>
<li id="blog"><a href="http://www.jiantong.cc">点一下又不会怀孕</a></li>
<li id="hexo"><a href="http://liujianpc.github.io">看一下也不会怀孕</a></li>
<li id="home"><a href="http://kindlers.sinaapp.com">带你飞</a></li>
<li id="wangyi"><a href="http://music.163.com">网易云音乐</a></li>
</ul>
</div>
</body>
</html>
<?php
header('Content-Type: text/html; charset=utf-8');
class NeteaseMusicAPI
{
private $ENCRYPT_N = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';
private $ENCRYPT_NONCE = '0CoJUm6Qyw8W8jud';
private $ENCRYPT_E='010001';
private $AES_VI='0102030405060708';
protected $_USERAGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.157 Safari/537.36';
protected $_REFERER='http://music.163.com/';
// key
protected $secretKey='TA3YiYCfY2dDJQgg';
protected $encSecKey='84ca47bca10bad09a6b04c5c927ef077d9b9f1e37098aa3eac6ea70eb59df0aa28b691b7e75e4f1f9831754919ea784c8f74fbfadf2898b0be17849fd656060162857830e241aba44991601f137624094c114ea8d17bce815b0cd4e5b8e2fbaba978c6d1d14dc3d1faf852bdd28818031ccdaaa13a6018e1024e2aae98844210';
private function prepare($raw)
{
$data['params'] =$this->aes_encode(json_encode($raw), $this->ENCRYPT_NONCE);
$data['params'] = $this->aes_encode($data['params'], $this->secretKey);
$data['encSecKey'] = $this->encSecKey;
return $data;
}
private function aes_encode($secretData, $secret)
{
return openssl_encrypt($secretData, 'aes-128-cbc', $secret, false, $this->AES_VI);
}
/**
* CURL 模块
* @param string $uri 目的地址
* @param string $postData POST数组
* @param string $cookie 携带Cookie
* @param string|array $header 自定义Header
* @return string
*/
protected function http_requests($uri, $postData = '', $cookie = '', $header = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($postData) { // post提交
if (is_array($postData)) $postData = http_build_query($postData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
if ($cookie) // 伪造cookie
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
if ($header) // 自定义header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public function url($song_id, $br = 999000){
$url = 'http://music.163.com/weapi/song/enhance/player/url?csrf_token=';
if (!is_array($song_id)) $song_id = [$song_id];
$data = [
'ids' => $song_id,
'br' => $br,
'csrf_token' => '',
];
return $this->http_requests(
$url,
$this->prepare($data),
'os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true',
[
'Origin: http://music.163.com',
'X-Real-IP: 183.30.197.115',
'Accept-Language: q=0.8,zh-CN;q=0.6,zh;q=0.2',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Referer: http://music.163.com/'
]
);
}
}
$a = new NeteaseMusicAPI();
$result = $a->url('18614850', 96000);
$array = json_decode($result, true);
var_dump($array);
?>
输出结果(php7 测试通过):
根据歌手id采集歌曲:
歌手ID大全:
两万多个,我就放我百度云吧,不然网页卡成狗
163.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>网易云音乐外链</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link href="./163music.css" rel="stylesheet">
<script src="//cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("script").last().remove();
$("div").last().remove();
var id = window.setTimeout(function () {
}, 0);
while (id--) {
window.clearTimeout(id);
}
});
</script>
<style type="text/css" media="screen" id="test">
</style>
</head>
<body>
<h2>查询结果如下</h2>
<div id="back"><a href="index.html">返回搜索</a></div>
<p>如果播放选项是灰色就表示这首歌是VIP专享</p>
<?php
session_start();//尝试通过session传递歌词参数
require 'Meting.php';
use Metowolf\Meting;
$api = new Meting('netease');
//header("Content-type:text/html;charset=utf-8");
function curl_get($url)
{
$refer = "http://music.163.com/";
$header[] = "Cookie: " . "appver=1.5.0.75771;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $refer);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$url = "http://music.163.com/api/search/get/web?csrf_token=";
$s = $_POST["search"];
$limit = 15;
function curl($url, $s, $limit)
{
$curl = curl_init();
$post_data = 'hlpretag=<span class="s-fc7">&hlposttag=</span>&s=' . $s . '&type=1&offset=0&total=true&limit=' . $limit;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$header = array(
'Host: music.163.com',
'Origin: http://music.163.com',
'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded',
'Referer: http://music.163.com/search/',
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$src = curl_exec($curl);
curl_close($curl);
return $src;
}
function netease_encryptId($id)
{
$magic = str_split('3go8&$8*3*3h0k(2)2');
$song_id = str_split($id);
for ($i = 0; $i < count($song_id); $i++) {
$song_id[$i] = chr(ord($song_id[$i]) ^ ord($magic[$i % count($magic)]));
}
$result = base64_encode(md5(implode('', $song_id), 1));
$result = str_replace(array('/', '+'), array('_', '-'), $result);
return $result;
}
//判断是否输入了歌曲名
if (!$s || !$limit) {
$tempArr = array("code" => -1, "msg" => "请输入歌曲名");
echo "<h2 style='color:yellow;text-align:center;'>" . "soory!" . $tempArr["msg"] . "!特么不输入歌曲名,怎么搜啊!!!" . "</h2>";
} else {
$ID_array = json_decode(curl($url, $s, $limit), true);//将json转换数组
echo "<table border='2'>";
echo "<tr>";
echo "<td>" . "歌曲序号" . "</td>";
echo "<td>" . "歌曲ID" . "</td>";
echo "<td>" . "歌曲名称" . "</td>";
echo "<td>" . "歌手" . "</td>";
echo "<td>" . "歌曲链接:" . "</td>";
echo "<td>" . "专辑封面:" . "</td>";
echo "<td>" . "专辑名:" . "</td>";
echo "<td>" . "歌词:" . "</td>";
echo "</tr>";
for ($i = 0; $i < $limit; $i++) {
$datas = $api->format(true)->song($ID_array["result"]["songs"][$i]["id"]);
$datas = json_decode($datas, true);
$url = 'https://p3.music.126.net/' . netease_encryptId($datas[0]['pic_id']) . '/' . $datas[0]['pic_id'] . '.jpg?param=200y200';
$data = $datas[0];
$lrc = $api->lyric($data['id']);
$lrc = json_decode($lrc, true);
echo "<tr>";
echo "<td>" . ($i + 1) . "</td>";
echo "<td>" . $ID_array["result"]["songs"][$i]["id"] . "</td>";
echo "<td>《" . $ID_array["result"]["songs"][$i]["name"] . "》</td>";//输出歌曲名
echo "<td>" . $ID_array["result"]["songs"][$i]["artists"][0]["name"] . "</td>";//输出歌手名
echo "<td> <audio src='http://music.163.com/song/media/outer/url?id={$ID_array["result"]["songs"][$i]["id"]}.mp3' controls='controls'></audio></td>";//通过歌曲id拼接出MP3歌曲链接
echo "<td><img src='$url' alt=''></td>";//专辑封面
echo "<td>{$datas[0]["album"]}</td>";//专辑名
$_SESSION["lyric" . $ID_array["result"]["songs"][$i]["id"]] = $lrc;
echo "<td><a href='lyric.php?num={$ID_array["result"]["songs"][$i]["id"]}' target='_blank'>查看歌词</a></td>";//歌词页
echo "</tr>";
}
echo "</table>";
}
?>
<div class="achor">
<ul>
<li id="blog"><a href="http://www.jiantong.cc">点一下又不会怀孕</a></li>
<li id="hexo"><a href="http://liujianpc.github.io">看一下也不会怀孕</a></li>
<li id="home"><a href="http://kindlers.sinaapp.com">带你飞</a></li>
<li id="wangyi"><a href="http://music.163.com">网易云音乐</a></li>
</ul>
</div>
</body>
</html>
新闻资讯 更多
- 【建站知识】查询nginx日志状态码大于400的请求并打印整行04-03
- 【建站知识】Python中的logger和handler到底是个什么?04-03
- 【建站知识】python3拉勾网爬虫之(您操作太频繁,请稍后访问)04-03
- 【建站知识】xpath 获取meta里的keywords及description的方法04-03
- 【建站知识】python向上取整以50为界04-03
- 【建站知识】scrapy xpath遇见乱码解决04-03
- 【建站知识】scrapy爬取后中文乱码,解决word转为html 时cp1252编码问题04-03
- 【建站知识】scrapy采集—爬取中文乱码,gb2312转为utf-804-03