PHPCrawl 크롤러 라이브러리는 Kugou 재생 목록 크롤링을 구현합니다.

小云云
풀어 주다: 2023-03-18 08:16:02
원래의
2039명이 탐색했습니다.

Crawler는 매우 흥미로운 기능입니다. 이 기사에서는 PHPCrawl 크롤러 라이브러리의 사용과 일반 매칭 관련 작업 기술을 포함하여 Kugou 재생 목록을 캡처하는 방법을 주로 소개합니다. 모두에게 도움이 됩니다.


<?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 크롤러 라이브러리는 Kugou 재생 목록 크롤링을 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!