PHPCrawl爬蟲庫抓取酷狗歌單

來源:互聯網
上載者:User
本人看了網路爬蟲相關的視頻後,蠢蠢欲動,也想爬點什麼。最近Facebook上表情包大戰很激烈,就想著把所有表情包都爬下來,卻一時沒有找到合適的VPN,因此只好仿照視頻爬歌單,把酷狗最近一月精選歌曲和簡單介紹抓取到本地。代碼寫得有點亂,自己不是很滿意,並不想放上來丟人現眼。不過轉念一想,這好歹是自己第一次爬蟲,記錄一下人生中的某個“第一次”有何不可?於是...就有了如下不堪入目的代碼~~~(ps.我是直接增、刪、改PHPCrawl爬蟲庫中給的example.php檔案。由於抓取的資料量較小,所以沒有考慮多進程什麼的,不過我看了一下PHPCrawl的文檔,發現PHPCrawl庫已經把我能想到的功能都封裝好了,實現起來很方便,YY著下次有時間就用它爬一些“大資料”,再用視覺化檢視進行資料分析,想想還有點小激動呢~)

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 "
").
if (PHP_SAPI == "cli") $lb = "\n";
else $lb = "
";

$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>([^( $ret = preg_match($pat,$content,$matches);
if($ret>0){
$songlistArr['title'] = $matches[1];
}else{
$songlistArr['title'] = '';
}
//解析歌曲
$pat = "/ $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 "

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

以上就介紹了PHPCrawl爬蟲庫抓取酷狗歌單,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.