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> ([^ (<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;?>