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 相关文章推荐
人大复印资料处理程序_补充篇
Oct 09 PHP
php+dojo 的数据库保存拖动布局的一个方法dojo 这里下载
Mar 07 PHP
PHP服务器页面间跳转实现方法
Aug 02 PHP
关于php循环跳出的问题
Jul 01 PHP
PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
May 10 PHP
php根据日期或时间戳获取星座信息和生肖等信息
Oct 20 PHP
变量在 PHP7 内部的实现(二)
Dec 21 PHP
PHP7安装Redis扩展教程【Linux与Windows平台】
Sep 30 PHP
PHP Socket网络操作类定义与用法示例
Aug 30 PHP
网站被恶意镜像怎么办 php一段代码轻松搞定(全面版)
Oct 23 PHP
php中的explode()函数实例介绍
Jan 18 PHP
如何在PHP中使用数组
Jun 09 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
PHP 金额数字转换成英文
2010/05/06 PHP
用php来限制每个ip每天浏览页面数量的实现思路
2015/02/24 PHP
PHP使用GETDATE获取当前日期时间作为一个关联数组的方法
2015/03/19 PHP
PHP的APC模块实现上传进度条
2015/10/27 PHP
关于ThinkPhp 框架表单验证及ajax验证问题
2017/07/19 PHP
JavaScript中的一些定位属性[图解]
2010/07/14 Javascript
使表格的标题列可左右拉伸jquery插件封装
2014/11/24 Javascript
详解JavaScript中setSeconds()方法的使用
2015/06/11 Javascript
关注jquery技巧提高jquery技能(前端开发必学)
2015/11/02 Javascript
Bootstrap 组件之按钮(二)
2016/05/11 Javascript
js实现碰撞检测特效代码分享
2016/10/16 Javascript
详解webpack+vue-cli项目打包技巧
2017/06/17 Javascript
详解Vue项目在其他电脑npm run dev运行报错的解决方法
2018/10/29 Javascript
微信小程序调用微信支付接口的实现方法
2019/04/29 Javascript
Vue+webpack实现懒加载过程解析
2020/02/17 Javascript
[01:04:20]完美世界DOTA2联赛PWL S2 LBZS vs Forest 第一场 11.29
2020/12/02 DOTA
python获取beautifulphoto随机某图片代码实例
2013/12/18 Python
python实现在目录中查找指定文件的方法
2014/11/11 Python
python 读取txt中每行数据,并且保存到excel中的实例
2018/04/29 Python
Django中使用第三方登录的示例代码
2018/08/20 Python
使用Python向DataFrame中指定位置添加一列或多列的方法
2019/01/29 Python
Python3模拟登录操作实例分析
2019/03/12 Python
python logging模块书写日志以及日志分割详解
2019/07/22 Python
Django的性能优化实现解析
2019/07/30 Python
python如何使用jt400.jar包代码实例
2019/12/20 Python
AUC计算方法与Python实现代码
2020/02/28 Python
python:HDF和CSV存储优劣对比分析
2020/06/08 Python
css3实现元素环绕中心点布局的方法示例
2019/01/15 HTML / CSS
Data URI scheme详解和使用实例及图片base64编码实现方法
2014/05/08 HTML / CSS
美国男女折扣服饰百货连锁店:Stein Mart
2017/05/02 全球购物
新春寄语大全
2014/04/09 职场文书
婚前财产公证书
2014/04/10 职场文书
2015年路政工作总结
2015/05/22 职场文书
电影地道战观后感
2015/06/04 职场文书
MySQL表类型 存储引擎 的选择
2021/11/11 MySQL
SQL Server 忘记密码以及重新添加新账号
2022/04/26 SQL Server