PHP爬虫音乐,PHPCrawl爬虫库实现抓取酷狗歌单

爬虫是一个很有意思的功能,本文主要介绍了PHPCrawl爬虫库实现抓取酷狗歌单的方法,涉及PHPCrawl爬虫库的使用及正则匹配相关操作技巧,需要的朋友可以参考下,希望能帮帮助到大家。

header("Content-type:text/html;charset=utf-8");

// It may take a whils to crawl a site ...

set_time_limit(10000);

include("libs/PHPCrawler.class.php");

class MyCrawler extends PHPCrawler {

function handleDocumentInfo($DocInfo) {

// Just detect linebreak for output ("\n" in CLI-mode, otherwise "
").

if (PHP_SAPI == "cli") $lb = "\n";

else $lb = "
";

$url = $DocInfo->url;

$pat = "/http:\/\/www\.kugou\.com\/yy\/special\/single\/\d+\.html/";

if(preg_match($pat,$url) > 0){

$this->parseSonglist($DocInfo);

}

flush();

}

public function parseSonglist($DocInfo){

$content = $DocInfo->content;

$songlistArr = array();

$songlistArr['raw_url'] = $DocInfo->url;

//解析歌曲介绍

$matches = array();

$pat = "/名称:([^(

$ret = preg_match($pat,$content,$matches);

if($ret>0){

$songlistArr['title'] = $matches[1];

}else{

$songlistArr['title'] = '';

}

//解析歌曲

$pat = "/

$matches = array();

preg_match_all($pat,$content,$matches);

$songlistArr['songs'] = array();

for($i = 0;$i < count($matches[0]);$i++){

$song_title = $matches[1][$i];

array_push($songlistArr['songs'],array('title'=>$song_title));

}

echo "

";

print_r($songlistArr);

echo "";

}

}

$crawler = new MyCrawler();

// URL to crawl

$start_url="http://www.kugou.com/yy/special/index/1-0-2.html";

$crawler->setURL($start_url);

// Only receive content of files with content-type "text/html"

$crawler->addContentTypeReceiveRule("#text/html#");

//链接扩展

$crawler->addURLFollowRule("#http://www\.kugou\.com/yy/special/single/\d+\.html$# i");

$crawler->addURLFollowRule("#http://www.kugou\.com/yy/special/index/\d+-\d+-2\.html$# i");

// Store and send cookie-data like a browser does

$crawler->enableCookieHandling(true);

// Set the traffic-limit to 1 MB(1000 * 1024) (in bytes,

// for testing we dont want to "suck" the whole site)

//爬取大小无限制

$crawler->setTrafficLimit(0);

// Thats enough, now here we go

$crawler->go();

// At the end, after the process is finished, we print a short

// report (see method getProcessReport() for more information)

$report = $crawler->getProcessReport();

if (PHP_SAPI == "cli") $lb = "\n";

else $lb = "
";

echo "Summary:".$lb;

echo "Links followed: ".$report->links_followed.$lb;

echo "Documents received: ".$report->files_received.$lb;

echo "Bytes received: ".$report->bytes_received." bytes".$lb;

echo "Process runtime: ".$report->process_runtime." sec".$lb;

?>

相关推荐:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值