hyperf+Querylist爬取音频网站 ,仅供交流学习请勿用于商业用途
直接上源码
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use QL\QueryList;
use Hyperf\DbConnection\Db;
class AduiosController extends AbstractController
{
public function huiyi(){
// 元数据采集规则
$rules = [
// 采集文章标题
'title' => ['.ellipsis>a','text'],
// 采集链接
'link' => ['.ellipsis>a','href'],
// 采集缩略图
'urlpath' => ['.music_block>p','thumb'],
];
// 切片选择器
$range = 'ul .b-box';
for ($i=1936; $i>=1; $i--){
$rt = QueryList::get("https://www.huiyi8.com/yinxiao/".$i.".html")->rules($rules)->range($range)->query()->getData();
$re = [];
foreach ($rt as $k=> $r){
$rs = QueryList::get($r['link']);
$eles=$rs->find('.c4095ce');
unset($r['link']);
$r['akey']=$eles->text();
if(strlen($r['akey'])>50){
$r['akey']=$r['title'];
}
// $r['audiopath']=$this->down_mp3($r['urlpath']);
$r['audiopath']="/audios/".pathinfo($r['urlpath'])['basename'];
$r['created_at']=time();
$re[]=$r;
}
$res=Db::table('audios')->insert($re);
var_dump($i);
}
}
function down_mp3($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$rawdata = curl_exec($ch);
curl_close($ch);
$filename=md5(pathinfo($url)['filename']."_".uniqid()).".mp3";
$file_path="./audios/".$filename;
$fp = fopen($file_path, 'w');
fwrite($fp, $rawdata);
fclose($fp);
return "/audios/".$filename;
}
public function setaudiopath(){
$audios=Db::table('audios')->where('audiopath',"")->limit(5000)->get(['id','urlpath']);
foreach ($audios as $i => $a){
$res = get_headers($a->urlpath,1);
$filesize = round($res['Content-Length']/1024,2);
if($filesize<10240){
$audiopath=$this->down_mp3($a->urlpath);
$res=Db::table('audios')->where('id',$a->id)->update(['audiopath'=>$audiopath]);
if($res){
echo($a->id);
}
}
}
}
}
衍生品: