今天來做一個PHP電影小爬蟲。
我們來利用simple_html_dom的采集數據實例,這是一個PHP的庫,上手很容易。
simple_html_dom 可以很好的幫助我們利用php解析html文檔。通過這個php封裝類可以很方便的解析html文檔,對其中的html元素進行操作 (PHP5+以上版本)
下載地址:https://github.com/samacs/simple_html_dom
下面我們以 http://www.paopaotv.com 上的列表頁 http://paopaotv.com/tv-type-id-5-pg-1.html 字母模式展現的列表為例,抓取頁面上的列表數據,以及內容裡面信息
<?php
include_once 'simple_html_dom.php';
//獲取html數據轉化為對象
$html = file_get_html('http://paopaotv.com/tv-type-id-5-pg-1.html');
//A-Z的字母列表每條數據是在id=letter-focus 的div內class= letter-focus-item的dl標簽內,用find方法查找即為
$listData=$html->find("#letter-focus .letter-focus-item");//$listData為數組對象
foreach($listData as$key=>$eachRowData){
$filmName=$eachRowData->find("dd span",0)->plaintext;//獲取影視名稱
$filmUrl=$eachRowData->find("dd a",0)->href;//獲取dd標簽下影視對應的地址
//獲取影視的詳細信息
// 查看本欄目