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与ASP
Oct 09 PHP
PHP类的静态(static)方法和静态(static)变量使用介绍
Feb 19 PHP
php生成随机数的三种方法
Sep 10 PHP
PHP实现Javascript中的escape及unescape函数代码分享
Feb 10 PHP
分享ThinkPHP3.2中关联查询解决思路
Sep 20 PHP
PHP attributes()函数讲解
Feb 03 PHP
Yii 使用intervention/image拓展实现图像处理功能
Jun 22 PHP
php判断/计算闰年的方法小结【三种方法】
Jul 06 PHP
laravel 实现划分admin和home 模块分组
Oct 15 PHP
laravel 错误处理,接口错误返回json代码
Oct 25 PHP
PHP策略模式写法
Apr 01 PHP
浅谈如何提高PHP代码质量之端到端集成测试
May 28 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
漂亮但不安全的CTB
2006/10/09 PHP
fleaphp crud操作之find函数的使用方法
2011/04/23 PHP
浅谈php中mysql与mysqli的区别分析
2013/06/10 PHP
jQuery的运行机制和设计理念分析
2011/04/05 Javascript
js简单实现用户注册信息的校验代码
2013/11/15 Javascript
js常用自定义公共函数汇总
2014/01/15 Javascript
js函数定时器实现定时读取系统实时连接数
2014/04/30 Javascript
浅析javascript操作 cookie对象
2014/12/26 Javascript
JavaScript 对象深入学习总结(经典)
2015/09/29 Javascript
js省市县三级联动效果实例
2020/04/15 Javascript
原生JS实现跑马灯效果
2017/02/20 Javascript
React Native时间转换格式工具类分享
2017/10/24 Javascript
微信小程序之圆形进度条实现思路
2018/02/22 Javascript
JavaScript观察者模式原理与用法实例详解
2020/03/10 Javascript
Python使用字典的嵌套功能详解
2019/02/27 Python
Python函数定义及传参方式详解(4种)
2019/03/18 Python
Windows下Pycharm远程连接虚拟机中Centos下的Python环境(图文教程详解)
2020/03/19 Python
Django+Celery实现动态配置定时任务的方法示例
2020/05/26 Python
基于python 取余问题(%)详解
2020/06/03 Python
Vs Code中8个好用的python 扩展插件
2020/10/12 Python
Selenium+BeautifulSoup+json获取Script标签内的json数据
2020/12/07 Python
python time.strptime格式化实例详解
2021/02/03 Python
python中操作文件的模块的方法总结
2021/02/04 Python
HTML5实现的震撼3D焦点图动画的示例代码
2019/09/26 HTML / CSS
超级英雄、电影和电视、乐队和音乐T恤:Loud Clothing
2019/09/01 全球购物
什么是Linux虚拟文件系统VFS
2015/08/25 面试题
小学红领巾中秋节广播稿
2014/01/13 职场文书
优秀高中生事迹材料
2014/02/11 职场文书
鼓舞士气的口号
2014/06/16 职场文书
应届本科毕业生求职信
2014/07/23 职场文书
兵马俑的导游词
2015/02/02 职场文书
优秀英文求职信范文
2015/03/19 职场文书
2015年企业员工工作总结范文
2015/05/21 职场文书
舞出我人生观后感
2015/06/16 职场文书
《领导干部从政道德启示录》学习心得体会
2016/01/20 职场文书
Python实现简单得递归下降Parser
2022/05/02 Python