Maison  >  Article  >  développement back-end  >  La bibliothèque de robots d'exploration PHPCrawl implémente l'exploration des listes de lecture Kugou

La bibliothèque de robots d'exploration PHPCrawl implémente l'exploration des listes de lecture Kugou

小云云
小云云original
2017-12-21 14:08:582040parcourir

Crawler est une fonction très intéressante.Cet article présente principalement la méthode d'exploration de la playlist Kugou par la bibliothèque de robots PHPCrawl, impliquant l'utilisation de la bibliothèque de robots PHPCrawl et des compétences opérationnelles liées à la correspondance régulière. J'espère que les amis qui en ont besoin pourront s'y référer. cela peut aider tout le monde.


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

Recommandations associées :

Partage d'expérience de démarrage avec les robots d'exploration Python

Qu'est-ce qu'un robot ? Quel est le processus de base du robot d’exploration ?

Tutoriel du robot d'exploration Web Python

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn