I conducted some testing and collected a dataset of songs from Yiyun – approximately over 20,000 artists and 10 million songs in total. Naturally, I have deleted all this data; the main purpose was to write some code for research, and I've also backed up the code here.
Collect songs based on singer ID:
隐藏内容:会员可查看
<?php
$shuju_file = "shuju.txt";
$shuju = @json_decode(@file_get_contents($shuju_file),true);
$id = empty($shuju['id'])?0:$shuju['id'];
$page = empty($shuju['page'])?0:$shuju['page'];
$urls = file_get_contents("songid.txt");
$array = explode(PHP_EOL,$urls);
$api = json_decode(file_get_contents("http://music.4s5.cn/artist/songs?id={$array[$id]}&limit=100&offset={$page}"),true);
echo "<p>当前歌手id:{$array[$id]}----歌手数".count($array)."----当前歌手序号:{$id}---当前歌手歌曲量:{$shuju['total']}---当前采集到歌手歌曲第{$shuju['page']}首</p>";
// die;
// 数据库信息
$host = "localhost"; // 数据库主机名
$username = "music163"; // 数据库用户名
$password = "dDHAHeiCR8rXyb68"; // 数据库密码
$database = "music163"; // 数据库名称
// 创建连接
$conn = new mysqli($host, $username, $password, $database);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(!empty($api['songs'])){
foreach ($api['songs'] as $v){
// print_r($v);
insertData($conn, "INSERT INTO `music163`.`hm_song` (`id`, `songid`, `song`) VALUES (NULL, '{$v['id']}', '".addcslashes($v['name'],"'")."');",$v['name']);
}
}
// 关闭连接
$conn->close();
function insertData($conn, $sql,$name = '') {
// 执行插入操作
if ($conn->query($sql) === TRUE) {
echo "<p style='color:green'>数据入库成功---{$name}"."</p>";
} else {
echo "<p style='color:red'>已采集过本歌曲: " . $conn->error ."----{$name}</p>";
}
}
// die;
$total = $api['total'];
if($shuju['page'] <= $total){
$shuju['page'] = $shuju['page'] + 100;
file_put_contents($shuju_file,json_encode([
'id' => $shuju['id'],
'page' => $shuju['page'],
'total' => $api['total'],
]));
go();
}else if(count($array) >= $shuju['id']){
$shuju['id']++;
file_put_contents($shuju_file,json_encode([
'id' => $shuju['id'],
'page' => 0,
'total' => $api['total'],
]));
echo "正在切换歌手.....";
go();
}else{
echo "所有歌手采集完成";
}
function go(){
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
echo '<meta http-equiv="refresh" content="'.mt_rand(0,1).'; url='.$url.'">';
}
Complete List of Singers' IDs:
两万多个,我就放我百度云吧,不然网页卡成狗