用perl抓取 网站缓存的音乐。

利用perl语言抓取脚本的时候,我是拒绝的。因为用perl语言太繁琐。空闲时间在听音乐,突然发现有些歌没办法听,就想起用perl抓个缓存音乐听听。看代码好了。

usestrict;

usewarnings;

useLWP::Simple;

useData::Dumper;

useHTTP::Request::Common;

useJSON;

useDigest::SHA;

useHTTP::Cookies;

useMIME::Base64;

useLWP::UserAgent;

useEncodeqw(decode encode);

my$V= LWP::UserAgent->new();

push@{ $V->requests_redirectable},’POST’;

$V->protocols_allowed([‘http’,’https’]);

$V->agent(“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36”);

#=========================================================

sub   search{

my$n=15;

my$M=shift();

chomp($M);

my$P=shift();

#P是换页;

#N是每以页显示的歌曲数;最多30首;

#M是搜索条件;

my$data;

my$sda=decode(‘gb2312’,$M);

my$rrr=$V->get(“https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.center&searchid=52186464057672763&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=”.$P.”&n=”.$n.”&w=”.$sda.”&g_tk=2036544290&jsonpCallback=searchCallbacksong9565&loginUin=854411658&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0″)->content;

my($ssjson) =$rrr=~ /^.*?\((.*)\)$/g;

#print encode(“gb2312”, decode(“utf-8”, $ssjson));

my$uir=decodeJSON($ssjson);

my$digit=0;

foreach my$element(@{$uir->{data}->{song}->{list}})

{

$digit++;

my$sng;

my$name=$element->{title};

my$list=$element->{file}->{media_mid};

print$digit.”.  “;

printencode(“gb2312”,decode(“utf-8″,”歌曲名字:”));

printencode(“gb2312”,decode(“utf-8”,$name));

my$song;

print”\n    “;

printencode(“gb2312”,decode(“utf-8″,”歌手:”));

foreach my$singer(@{$element->{singer}})

{

my$se=encode(“gb2312”,decode(“utf-8”,$singer->{title}));

push(@{$song},$se);

print$se;

print”  “;

}

print”\n”;

#print Dumper $list;

print”\n”;

print”\n”;

$sng->{url} =$list;

$sng->{title} =$song;

$sng->{singer} =encode(“gb2312”,decode(“utf-8”,$name));

push(@{$data},$sng);

}

printencode(“gb2312”,decode(“utf-8″,”本页有”.$digit.”首歌,这是第”.$P.”页\n”));

print”=========================================================\n\n”;

return$data;

}

#=========================================================

sub   download{

my$int=shift();

my$data=shift();

#print Dumper $data;

my$h=$V->get(“https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?g_tk=737013179&jsonpCallback=MusicJsonCallback778146979323697&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0&cid=205361747&callback=MusicJsonCallback778146979323697&uin=854411658&songmid=0020UhY82Uhzvq&filename=C400″.@{$data}[$int]->{url}.”.m4a&guid=4605006430″)->content;

my($sssjson) =$h=~ /^.*?\((.*)\)$/g;

my$sss=decodeJSON($sssjson);

my$id=@{$sss->{data}->{items}}[0]->{vkey};

my$e=$V->get(“http://dl.stream.qqmusic.qq.com/C400″.@{$data}[$int]->{url}.”.m4a?vkey=”.$id.”&guid=4605006430&uin=854411658&fromtag=66″)->content;

open(FILE,”>”.@{$data}[$int]->{singer}.”.mp3″);

binmodeFILE;

printFILE$e;

close(FILE);

printencode(“gb2312”,decode(“utf-8″,”下载完成\n\n”));

}

#=============================================================

printencode(“gb2312”,decode(“utf-8″,”请输入搜索关键词 :”));

my$dde= ;

chomp($dde);

my$name=$dde;

my$int=1;

my$data=search($dde,$int);

for(my$r=1;$r>0;$r++){

printencode(“gb2312”,decode(“utf-8″,”请进行编辑,下一页输入(next),上一页请输入(previous),下载请输入(dow+下载的歌曲序号),重新搜索请输入(again),停止运行请输入(exit)\n\n”));

my$dede= ;

print”\n”;

chomp($dede);

if($dede=~ /next/g){

$int++;

$data=search($name,$int);

}elsif($dede=~ /previous/g){

$int–;

if($int<=0){

printencode(“gb2312”,decode(“utf-8″,”输入错误。\n”));

}else{

$data=search($name,$int);

}

}elsif($dede=~ /dow(\d+?)/g){

if($1>15){

printencode(“gb2312”,decode(“utf-8″,”输入错误。\n”));

}else{

download($1-1,$data);

}

}elsif($dede=~ /exit/g){

printencode(“gb2312”,decode(“utf-8″,”正在停止……..\n”));

last;

}elsif($dede=~ /again/g){

printencode(“gb2312”,decode(“utf-8″,”请输入搜索关键词 :”));

my$de= ;

print”\n”;

chomp($de);

$name=$de;

$data=search($de,$int)

}else{

printencode(“gb2312”,decode(“utf-8″,”输入错误。\n”));

}

}

printencode(“gb2312”,decode(“utf-8″,”已退出程序”));

sub  sprintJSON{

my$json= JSON->new->allow_nonref;

return$json->encode(shift());

}

sub  decodeJSON{

my$json= JSON->new->allow_nonref;

return$json->decode(shift());

}

脚本是通过控制台运行。只有一个搜索和下载,其他的没完善。

    原文作者:自闭症小孩
    原文地址: https://www.jianshu.com/p/3145c8594b22
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞