Phpcrawl Crawler library for capturing cool dog songs

Source: Internet
Author: User
Crawler is a very interesting feature, this article mainly introduces the Phpcrawl Crawler Library to achieve the capture of Cool dog song single method, involving the use of Phpcrawl Crawler library and regular matching related operation skills, the need for friends can refer to, hope to help everyone.


<?phpheader ("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) {//J    UST 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 ();    The Public Function parsesonglist ($DocInfo) {$content = $DocInfo->content;    $SONGLISTARR = Array ();    $SONGLISTARR [' raw_url '] = $DocInfo->url;    Parse Song Introduction $matches = Array ();    $pat = "/<span> name:<\/span> ([^ (&LT;BR)]+) <br/";    $ret = Preg_match ($pat, $content, $matches);    if ($ret >0) {$songlistArr [' title '] = $matches [1];    }else{$songlistArr [' title '] = '; }//Parse song $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#");//Link extension $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-l Imit to 1 MB (on bytes,//for testing we dont wAnt to "suck" the whole site)//Crawl size unrestricted $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;?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.