爬虫是一个很有意思的功能,本文主要介绍了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;

?>

相关推荐:

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

  1. 爬虫训练(三):爬取酷狗音乐

    今天趁机一鼓作气,把简单爬虫内容一次学习完毕,最后以爬取酷狗音乐排行榜歌曲作为结束,然后对此次学习做一个整理和总结.而且前两篇有些混乱,这里把内容做一次阶段性总结. 一.安装包 爬虫三大包:reque ...

  2. 【python实现网络爬虫(14)】python爬取酷狗中多类型音乐步骤详解(附全部源代码)

    目标网址:酷狗音乐-赤伶,页面如下 爬虫逻辑: [找到要获取特定音乐的url]>>>[找到该资源链接的url]>>>[封装获取音乐的函数]>>>[ ...

  3. PHPCrawler抓取酷狗精选集歌单

    一.PHPCrawler的介绍与安装 先了解一下什么是抓取? 抓取就是网络爬虫,也就是人们常说的网络蜘蛛(spider).是搜索引擎的一个重要组成部分,按照一定的逻辑和算法抓取和下载互联网上的信息和网 ...

  4. python爬取酷狗音乐的mv地址_爬取酷狗音乐.py

    import requests from selenium import webdriver from pyquery import PyQuery as pq #获取歌名 def Access_to ...

  5. Python抓取酷狗音樂存入mongDB中

    #//div[@class='pc_temp_songlist']/ul/li/ span.pc_temp_num and a import requests from bs4 import Beau ...

  6. Python疫起学习·万丈高楼平地起Day09(精简版|浓缩就是精华)爬虫知识附上案例爬取北京地区短租房信息、爬取酷狗TOP500的数据以及爬取网易云音乐热歌榜单

    爬虫知识 Requests库 部分运行结果如下: 有时爬虫需要加入请求头来伪装成浏览器,以便更好地抓取数据.在Chrome浏览器中按F12键打开Chrome开发者工具,刷新网页后找到User-Agen ...

  7. Java爬虫系列之实战:爬取酷狗音乐网 TOP500 的歌曲(附源码)

    在前面分享的两篇随笔中分别介绍了HttpClient和Jsoup以及简单的代码案例: Java爬虫系列二:使用HttpClient抓取页面HTML Java爬虫系列三:使用Jsoup解析HTML 今天 ...

  8. Python爬虫入门——2. 2爬取酷狗音乐top1-500歌曲信息

    有了第一个程序的基础,我们现在来爬取酷狗音乐top500的歌曲信息.连接http://www.kugou.com/yy/rank/home/1-8888.html 我们第一个程序只爬取了一个页面的数据 ...

  9. Java爬虫——爬取网易云歌单音乐添加到QQ音乐

    此博客仅为学习交流,如触及第三方利益,请及时联系本人删除 一.前言 看标题大家可能会有点疑惑,为什么要写这个看起来没什么作用的爬虫,两个音乐软件换着用不香吗?     基于此问题,我以我个人感受罗列了 ...

最新文章

  1. 【T-SQL】基础——表别名
  2. hdu5437(2015长春网络赛A题)
  3. 【数据结构与算法】之深入解析“分割数组的最大值”的求解思路与算法示例
  4. 怎么让电脑屏幕一直亮着_笔记本开机白屏怎么回事 笔记本开机白屏解决方法【详解】...
  5. 计算机系统基础:文件管理相关知识笔记
  6. d3 tip mysql_mysql
  7. Android Drawable绘图学习笔记
  8. uniapp小程序的getsysteminfo_Typecho小程序:Pisces-Mini-Program
  9. 【Siddhi】Syntax error in SiddhiQL, no viable alternative at input
  10. 操作系统知识点大总结【管程与死锁】
  11. Publication的 immediate_sync 属性
  12. 机器学习基础:期望最大化算法(Machine Learning Fundamentals: EM Algorithm)
  13. 数据抓取的艺术(三)
  14. python @符号_注意!大佬提醒你python初学者这几个很难绕过的坑,附教程资料
  15. Unity接入谷歌支付
  16. 【Android】使用MPAndroidChart过程中BarChart的几点心得
  17. Circuit Design 贴片晶振的区分
  18. pancakeswap 开盘抢跑机器人 (附代码)
  19. AMR文件时长解析——附c代码
  20. 【无人驾驶系列五】GPS及惯性传感器在无人驾驶中的应用

热门文章

  1. phpexcel导出大量数据合并单元格_php 数据导出到excel 2种带有合并单元格的导出...
  2. c语言中合法转义字符,判断c语言合法转义字符
  3. 关于修改了virc(增加了:set nu)但是vim行号还是不显示的问题
  4. ASP.NET Core 依赖注入
  5. Spring的两种动态代理:Jdk和Cglib 的区别和实现
  6. supervisor开机自启动方法
  7. Python__模拟实现一个ATM+购物商城程序
  8. tableview的顶部有一部分空白区域,并不是Cell的解决方法。
  9. 深入理解JavaScript闭包(closure) 【收藏】
  10. C#中使用MD5对用户密码加密与解密