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


Posted in PHP onDecember 21, 2017

本文实例讲述了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; 
?>
PHP 相关文章推荐
PHP开发环境配置(MySQL数据库安装图文教程)
Apr 28 PHP
PHP gbk环境下json_dencode传送来的汉字
Nov 13 PHP
PHP开发框架kohana3 自定义路由设置示例
Jul 14 PHP
php生成QRcode实例
Sep 22 PHP
windows下配置php5.5开发环境及开发扩展
Dec 25 PHP
PHP实现自动识别Restful API的返回内容类型
Feb 07 PHP
php根据某字段对多维数组进行排序的方法
Mar 07 PHP
DEDECMS首页调用图片集里的多张图片
Jun 05 PHP
joomla实现注册用户添加新字段的方法
May 05 PHP
谈谈php对接芝麻信用踩的坑
Dec 01 PHP
php实例化一个类的具体方法
Sep 19 PHP
TP5框架请求响应参数实例分析
Oct 17 PHP
在云虚拟主机部署thinkphp5项目的步骤详解
Dec 21 #PHP
php获取微信共享收货地址的方法
Dec 21 #PHP
php实现socket推送技术的示例
Dec 20 #PHP
PHP实现模拟http请求的方法分析
Dec 20 #PHP
php封装db类连接sqlite3数据库的方法实例
Dec 19 #PHP
PHP性能分析工具xhprof的安装使用与注意事项
Dec 19 #PHP
PHP实现的最大正向匹配算法示例
Dec 19 #PHP
You might like
WML,Apache,和 PHP 的介绍
2006/10/09 PHP
MySQL修改密码方法总结
2008/03/25 PHP
php分页思路以及在ZF中的使用
2012/05/30 PHP
Zend Framework教程之Zend_Config_Xml用法分析
2016/03/23 PHP
javascript onmouseout 解决办法
2010/07/17 Javascript
Jquery读取URL参数小例子
2013/08/30 Javascript
jquery三个关闭弹出层的小示例
2013/11/05 Javascript
减少访问DOM的次数提升javascript性能
2014/02/24 Javascript
JS实现的左侧竖向滑动菜单效果代码
2015/10/19 Javascript
Bootstrap中glyphicons-halflings-regular.woff字体报404错notfound的解决方法
2017/01/19 Javascript
nodejs学习笔记之路由
2017/03/27 NodeJs
浅谈Vuejs Prop基本用法
2017/08/17 Javascript
使用DataTable插件实现异步加载数据
2017/11/19 Javascript
js时间戳与日期格式之间相互转换
2017/12/11 Javascript
详解ES6系列之私有变量的实现
2018/11/21 Javascript
15分钟上手vue3.0(小结)
2020/05/20 Javascript
[44:37]完美世界DOTA2联赛PWL S3 Forest vs access 第一场 12.11
2020/12/13 DOTA
讲解python参数和作用域的使用
2013/11/01 Python
树莓派中python获取GY-85九轴模块信息示例
2013/12/05 Python
Python 中开发pattern的string模板(template) 实例详解
2017/04/01 Python
Python利用递归和walk()遍历目录文件的方法示例
2017/07/14 Python
pycharm重置设置,恢复默认设置的方法
2018/10/22 Python
python 使用装饰器并记录log的示例代码
2019/07/12 Python
Django 限制访问频率的思路详解
2019/12/24 Python
pytorch之ImageFolder使用详解
2020/01/06 Python
通过实例解析Python return运行原理
2020/03/04 Python
python根据完整路径获得盘名/路径名/文件名/文件扩展名的方法
2020/04/22 Python
keras:model.compile损失函数的用法
2020/07/01 Python
浅谈amaze-ui中datepicker和datetimepicker注意的几点
2020/08/21 HTML / CSS
售后专员岗位职责
2013/12/08 职场文书
面临毕业的毕业生自荐书范文
2014/02/05 职场文书
经典团队口号大全
2014/06/21 职场文书
2014年度培训工作总结
2014/11/27 职场文书
小学校长开学致辞
2015/07/29 职场文书
《水上飞机》教学反思
2016/02/20 职场文书
解析redis hash应用场景和常用命令
2021/08/04 Redis