在網上看到很多簡單的采集教程,尤其是針對圖書網站的比較多,但附帶實例的并不多,在看了一篇針對八路中文網的抓取分析后,決定針對這個網站,寫一個簡單的抓取教程,并附帶實例。由于俺偷懶,文中很多分析都是來自《利用php制作簡單的內容采集器》,俺只是進一步優化了他的流程,并完成了代碼實例的編寫。
采集程序其實并不難做,只要分析清楚流程,然后使用合適的正則來取到你想要的內容就可以了。廢話不說了,教程開始:
1.分析入口:
多打開幾本書后,可以發現書名的基本格式是:http://www.86zw.com/book/書號/index.aspx。于是得出:
代碼:
$bookid='1888';
$index="http://www.86zw.com/book/".$bookid."/index.aspx";//組合書目首頁url
2.打開頁面:
代碼:
$contents=file_get_contents($index);
3.抓取圖書信息頁:
代碼:
//抓取圖書相關信息
preg_match_all("/<div id=/"crbooktitle/"><span class=/"booktitle/">(.*)<//span><//div>/is",$contents,$arraytitle);
preg_match_all("/【<a href=/"(.*)/"><font color=/"#cc0000/">點擊閱讀<//font><//a>】/is",$contents,$arraylist);
unset($contents);
$title=$arraytitle[1][0];//書名
$list="http://www.86zw.com".trim($arraylist[1][0]);//列表頁url
4.創建保存目錄及文件:
代碼:
//生成文本文檔名稱
$txt_name=$title.".txt";
creatdir($bookid);//創建圖片文件夾
writestatistic($title."/r/n",$txt_name);//圖書標題寫入文本文件
5.進入列表頁:
代碼:
//進入列表頁
$list_contents=file_get_contents($list);
6.抓取列表頁章節:
代碼:
//進入列表頁
//分章節抓塊
preg_match_all("|<div id=/"nclasstitle/">(.*) 【<a href=/"(.*)/">分卷閱讀<//a>】<//div>(.*)<div id=/"listend/"><//div>|uis",$list_contents,$block);
//計算總章節數
$regcount=count($block[0]);
7.分章節進行抓取:
代碼:
//進入章節
for($pagebooknum=0;$pagebooknum<$regcount;$pagebooknum++){
unset($zhang);
unset($list_url);
$zhang=$block[1][$pagebooknum];//章節標題
writestatistic('章節:'.($pagebooknum+1).' '.$zhang."/r/n",$txt_name);//章節標題寫入文本文件
preg_match_all("|<li><a href=/"(.*)/" title=/"(.*)/">(.*)<//a><//li>|uis",$block[3][$pagebooknum],$list_url);
//進入頁面
for($listnum=0;$listnum<count($list_url[1]);$listnum++){
unset($book_url);
unset($book);
unset($book_contents);
unset($book_time);
unset($book_title);
$book_time=$list_url[2][$listnum];//小章節更新信息
$book_title=$list_url[3][$listnum];//小章節標題
$book_url=preg_replace("'index.shtm'si",$list_url[1][$listnum],$list);//小章節鏈接url
writestatistic(($listnum+1).'.'.$book_title.'-'.$book_time."/r/n",$txt_name);//小章節標題寫入文本文件
$book=file_get_contents($book_url);
//抓取圖書內容
preg_match_all("/<div id=/"booktext/">(.*)<iframe id=hkwxc/is",$book,$arraycontents);
$book_contents=preg_replace("|<div style='display:none'>.*<//div>|uis",'',$arraycontents[1][0]);
//$book_contents=preg_replace("|<br />|uis",'/n/r',$book_contents);
//$book_contents=preg_replace("|<br>|uis",'/n/r',$book_contents);
$book_contents=preg_replace("| |uis",' ',$book_contents);
$book_contents=strip_tags($book_contents);
//判斷圖片頁面
if (preg_match ("/<div align=/"center/"><img src=/".*/" id=/"imgbook/" name=/"imgbook/" border=/"0/" //><//div>/i", $book_contents)) {
//取圖片url
preg_match_all("|<div align=/"center/"><img src=/"(.*)/" id=/"imgbook/" name=/"imgbook/" border=/"0/" //><//div>|uis",$book_contents,$images);
//取圖片
for($imagenum=0;$imagenum<count($images[1]);$imagenum++){
unset($image_url);
$image_url="http://www.86zw.com".trim($images[1][$imagenum]);
$new_url='image/'.$bookid.'/'.time().'.gif';
//復制圖片并生成圖片連接
if (copy($image_url, $new_url)){
$book_contents.="<img src=$new_url>";
}
}//取圖片結束
}//圖片判斷結束
writestatistic($book_contents,$txt_name);//內容寫入文本文件
}//頁面循環結束
}//章節循環結束
兩個使用的函數:
代碼:
/**
* 將內如寫入指定文件包
*
* 參數: string $sql : 寫入的內容
string $txt_name : 指定文件名
* 返回: void
* 作用域: public
* 日期: 2007-11-29
*/
function writestatistic($sql,$txt_name){
$filename="txt_packet/".$txt_name;//注意修改文件的路徑
if (file_exists($filename)) {
$fp=fopen($filename,"a");
}else{
$fp=fopen($filename,"w");
}
$text=$sql;
fwrite($fp,$text);
fclose($fp);
}
/**
* 創建文件夾
*
* 參數: string $bookid : 指定文件夾名
* 返回: void
* 作用域: public
* 日期: 2007-11-29
*/
function creatdir($bookid){
$filename="image/".$bookid;//注意修改文件的路徑
if (!file_exists($filename)) {
mkdir($filename,0777);
}
}
自此完成一本書的簡單采集。
寫這個就是為了給想了解采集的phper一個簡單的實例,采集其實很簡單。。。