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;
?>

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/6464.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
重蔚重蔚管理团队
上一篇 2018年4月25日 09:18
下一篇 2018年4月25日 09:29

相关推荐

  • PHP8.0中的类型约束处理库:Typehint

    PHP是一门非常受欢迎的编程语言,被广泛用于Web开发领域。随着版本的不断更新,PHP的功能也日益丰富。在2020年11月,PHP8.0版本正式发布,其中一个重要的新特性就是类型约束处理库——Typehint。Typehint是PHP8.0中新…

    2023年5月18日
    03
  • 我来教你lnmp是什么意思。

    【lnmp是什么意思】:LAMP、Nginx、MySQL和PHP的组合,其中L表示Linux操作系统,M表示MySQL数据库,N表示Nginx Web服务器,P表示PHP编程语言,lnmp是Linux、Nginx、MySQL和PHP的缩写,这个组合是一种常见的Web服务…

    2024年6月18日
    01
  • PHP文件操作相关函数

    bool copy ( string filename , string dest ) 复制文件 string filename:原文件 string dest :目标文件 bool unlink ( string filename ) 删除文件 string filename:要删除的文件 bool rename ( string oldname…

    2017年11月15日
    0356
  • PHP实现数据库分表故障恢复的方法。

    随着电子商务和互联网技术的快速发展,互联网应用的性能和可靠性成为了最重要的指标之一。而对于数据库来说,性能和可靠性也是至关重要的。其中一个重要的问题就是数据量过大导致单表数据量过大,引发性能问题。为…

    2023年5月21日
    09
  • Cookie的应用案例

    显示用户访问时间,如果是第一次访问服务器,显示第一次访问,时间为xxx;如果是不是第一次访问,则显示上次访问时间为xxx 思考:想要读取到上次访问这个页面时的时间,由于http协议无状态的,所以默认情况下无法读…

    2018年9月13日 PHP自学教程
    0248
  • PHP可变变量的理解

    可变变量 所谓可变变量,就是一个变量的名,又是一个变量。 可变变量的语法是php的很特殊的语法——其他语言中少见。 $v1 = “abc”;          //这是一个字符串变量,其内容是字符串“abc” $abc = 10;             //这…

    2017年11月6日
    0462
  • PHP中使用Redis实现分级存储。

    随着互联网业务的快速发展,数据量的增长速度也越来越快。在这样大规模的数据处理中,如何高效地存储和快速访问数据成为了一个亟待解决的问题。传统的关系型数据库存储方式已经无法满足需要,因此,非关系型存储系…

    2023年5月21日
    03
  • 详解PHP中错误与异常及其相关知识

    PHP错误级别Parse error > Fatal Error > Waning > Notice > DeprecatedDeprecated 最低级别的错误(不推荐,不建议)使用一些过期函数的时候会出现,程序继续执行Notice 通知级别的错误使用一些未定义变…

    2022年6月12日
    0164

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息