php网页采集器 源码,PHP采集器的简单示例代码
復制代碼 代碼示例:
#zs#*
* 采集器代碼一例
* by www.jbxue.com
#fzs#
$url = "http://book.sina.com.cn/nzt/lit/zhuxian2/index.shtml";// 圖書地址
$ver = "old"; //新舊版本
$r = file_get_contents($url); //用file_get_contents將網址打開并讀取所打開的頁面的內容
preg_match("//is",$r,$booktitle);//匹配此頁面的標題
$bookname = $booktitle[1];//取第二層數組
$preg = '/
/isU';preg_match_all($preg, $r, $zj); //將此頁面的章節連接匹配出來
$bookzj = count($zj[1]);// 計算章節標題數量
if ($ver=="new"){
$content_start = "";
$content_end = "";
}
if ($ver=="old"){
$content_start = "";
$content_end = "
";
}
header("Content-Type:text/html;charset=gb2312");
writer($bookname." 共".$bookzj."節rn帥哥劉并于".date("D M j G:i:s T Y")."為了畢業而設計小說整理收集rn", "./ailaopo/".$bookname.".txt","w+");
for ($i=0;$i
//echo "http://book.sina.com.cn".$zj[1][$i]".shtml";die();
//用file_get_contents將章節連接打開并讀取所打開的頁面的內容
$str = file_get_contents("http://book.sina.com.cn".$zj[1][$i].".shtml");
preg_match("/(
)(.*?)()/is",$str,$title);//匹配此連接頁面的標題$title = str_replace("_讀書頻道_新浪網","",$title[2]);//把$title[2]里面有_讀書頻道_新浪網的換成空
preg_match("/(".$content_start.")(.*?)(".$content_end.")/is",$str,$content);//匹配此連接頁面的內容
$content = preg_replace("//s","",str_replace("
","rn",$content[2]));//用str_replace把$content[2]里有 的換成rnprint_r($content);
exit;
//把第".($i+1)."節和標題與內容連接在一起放在變量
$result = " rn第".($i+1)."節--------".$title."_汪老師就是帥 --------- rn".$content;
writer($result, "./ailaopo/".$bookname.".txt","a+");//調用函數把$result
echo "小說".$bookname."共".$bookzj."節,現在整理到第".$i."節 _".$title."
";
}
echo "小說".$bookname."共".$bookzj."節 已全部整理完成!";
function writer($content,$url,$mode)//定義函數名 writer 參數$content,$url,$mode
{
$fp = fopen($url, $mode);//打開文件$url
fwrite($fp, $content);//把$content放入到$fp
fclose($fp); //關閉$fp
}
?>
您可能感興趣的文章:
總結
以上是生活随笔為你收集整理的php网页采集器 源码,PHP采集器的简单示例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript 原型链和继承面试题
- 下一篇: oracle集群搭建视频,Oracle