久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHPCrawl爬蟲庫實現(xiàn)抓取酷狗歌單的方法示例

這篇文章主要介紹了PHPCrawl爬蟲庫實現(xiàn)抓取酷狗歌單的方法,涉及PHPCrawl爬蟲庫的使用及正則匹配相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了PHPCrawl爬蟲庫實現(xiàn)抓取酷狗歌單的方法。分享給大家供大家參考,具體如下:

本人看了網(wǎng)絡(luò)爬蟲相關(guān)的視頻后,手癢癢,想爬點(diǎn)什么。最近Facebook上表情包大戰(zhàn)很激烈,就想著把所有表情包都爬下來,卻一時沒有找到合適的VPN,因此把酷狗最近一月精選歌曲和簡單介紹抓取到本地。代碼寫得有點(diǎn)亂,自己不是很滿意,并不想放上來丟人現(xiàn)眼。不過轉(zhuǎn)念一想,這好歹是自己第一次爬蟲,于是...就有了如下不堪入目的代碼~~~(由于抓取的數(shù)據(jù)量較小,所以沒有考慮多進(jìn)程什么的,不過我看了一下PHPCrawl的文檔,發(fā)現(xiàn)PHPCrawl庫已經(jīng)把我能想到的功能都封裝好了,實現(xiàn)起來很方便)


<?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['raw_url'] = $DocInfo->url;
    //解析歌曲介紹
    $matches = array();
    $pat = "/<span>名稱:<\/span>([^(<br)]+)<br/";
    $ret = preg_match($pat,$content,$matches);
    if($ret>0){
      $songlistArr['title'] = $matches[1];
    }else{
      $songlistArr['title'] = '';
    }
    //解析歌曲
    $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#");
//鏈接擴(kuò)展
$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 = "<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; 
?>

PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:

JavaScript正則表達(dá)式在線測試工具:
http://tools.html5code.net/regex/javascript

正則表達(dá)式在線生成工具:
http://tools.html5code.net/regex/create_reg

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php正則表達(dá)式用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計有所幫助。

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

主站蜘蛛池模板: 岛国av一区二区三区 | 欧美日韩在线综合 | 日韩成人av在线 | 色婷婷一区二区三区四区 | 久久男女视频 | 欧美亚洲视频 | 91久久久久 | 日韩在线一区二区三区 | 欧美精品一区二区三区四区五区 | 日韩福利一区 | 91精品国产色综合久久不卡98 | 久久国产成人精品国产成人亚洲 | 久久久久国产精品午夜一区 | 国产精品一区久久久 | 国产精品三级久久久久久电影 | 国产一级淫片a直接免费看 免费a网站 | 91日b| 超碰在线播| 亚洲国产精品一区二区久久 | 久久久久久中文字幕 | aaa级片| 久在线视频 | 欧美国产精品 | 成人免费大片黄在线播放 | 欧洲一区二区三区 | 国产99视频精品免费视频7 | 国产精品海角社区在线观看 | 欧美一二三四成人免费视频 | 日韩精品一区二区三区视频播放 | 99久久精品国产毛片 | 精品国产乱码久久久久久图片 | 久久亚洲精品久久国产一区二区 | 亚州综合在线 | 欧美一级在线观看 | 亚洲精品视频一区二区三区 | 特黄色一级毛片 | 日本三级电影在线看 | 欧美日韩精品亚洲 | 欧美午夜精品 | 欧美日韩在线视频一区二区 | 久久久精彩视频 |