PHPCrawl爬蟲庫實作抓取酷狗歌單

小云云
發布: 2023-03-18 08:16:02
原創
2103 人瀏覽過

爬蟲是一個很有意思的功能,本文主要介紹了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[&#39;raw_url&#39;] = $DocInfo->url;
    //解析歌曲介绍
    $matches = array();
    $pat = "/<span>名称:<\/span>([^(<br)]+)<br/";
    $ret = preg_match($pat,$content,$matches);
    if($ret>0){
      $songlistArr[&#39;title&#39;] = $matches[1];
    }else{
      $songlistArr[&#39;title&#39;] = &#39;&#39;;
    }
    //解析歌曲
    $pat = "/<a title=\"([^\"]+)\" hidefocus=\"/";
    $matches = array();
    preg_match_all($pat,$content,$matches);
    $songlistArr[&#39;songs&#39;] = array();
    for($i = 0;$i < count($matches[0]);$i++){
      $song_title = $matches[1][$i];
      array_push($songlistArr[&#39;songs&#39;],array(&#39;title&#39;=>$song_title));
    }
    echo "<pre class="brush:php;toolbar:false">";
    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; ?>
登入後複製

相關推薦:

##Python爬蟲入門心得分享

什麼是爬蟲?爬蟲的基本流程是什麼?

python之網頁爬蟲教學

以上是PHPCrawl爬蟲庫實作抓取酷狗歌單的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板