欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHPCrawl爬虫库实现抓取酷狗歌单的方法示例

程序员文章站 2022-11-20 08:21:04
本文实例讲述了phpcrawl爬虫库实现抓取酷狗歌单的方法。分享给大家供大家参考,具体如下: 本人看了网络爬虫相关的视频后,手痒痒,想爬点什么。最近facebook上表情...

本文实例讲述了phpcrawl爬虫库实现抓取酷狗歌单的方法。分享给大家供大家参考,具体如下:

本人看了网络爬虫相关的视频后,手痒痒,想爬点什么。最近facebook上表情包大战很激烈,就想着把所有表情包都爬下来,却一时没有找到合适的vpn,因此把酷狗最近一月精选歌曲和简单介绍抓取到本地。代码写得有点乱,自己不是很满意,并不想放上来丢人现眼。不过转念一想,这好歹是自己第一次爬虫,于是...就有了如下不堪入目的代码~~~(由于抓取的数据量较小,所以没有考虑多进程什么的,不过我看了一下phpcrawl的文档,发现phpcrawl库已经把我能想到的功能都封装好了,实现起来很方便)

<?php
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 "<br>").
    if (php_sapi == "cli") $lb = "\n";
    else $lb = "<br />";
    $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 = "/<span>名称:<\/span>([^(<br)]+)<br/";
    $ret = preg_match($pat,$content,$matches);
    if($ret>0){
      $songlistarr['title'] = $matches[1];
    }else{
      $songlistarr['title'] = '';
    }
    //解析歌曲
    $pat = "/<a title=\"([^\"]+)\" hidefocus=\"/";
    $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 "<pre>";
    print_r($songlistarr);
    echo "</pre>";
    }
  }
$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 = "<br />";
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; 
?>

ps:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

javascript正则表达式在线测试工具:

正则表达式在线生成工具:

更多关于php相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。