Home > php教程 > PHP源码 > A simple PHP collector

A simple PHP collector

大家讲道理
Release: 2016-11-09 10:08:18
Original
1422 people have browsed it

<?php
 
//****************************************************************
$url = "http://book.sina.com.cn/nzt/lit/zhuxian2/index.shtml";// 图书地址
$ver = "old"; //新旧版本
//****************************************************************
 
$r = file_get_contents($url); //用file_get_contents将网址打开并读取所打开的页面的内容
preg_match("/<meta name=\"description\" content=\"(.*?)\">/is",$r,$booktitle);//匹配此页面的标题
$bookname = $booktitle[1];//取第二层数组
$preg = &#39;/<li><a href=(.*).shtml target=_blank class=a03>/isU&#39;;
preg_match_all($preg, $r, $zj); //将此页面的章节连接匹配出来
$bookzj = count($zj[1]);// 计算章节标题数量
if ($ver=="new"){
$content_start = "<!--正文内容开始-->";
$content_end = "<!--正文内容结束-->";
}
if ($ver=="old"){
$content_start = "<\/table><!--NEWSZW_HZH_END-->";
$content_end = "<br>";
}
 
header("Content-Type:text/html;charset=gb2312");
 
writer($bookname." 共".$bookzj."节\r\n帅哥刘并于".date("D M j G:i:s T Y")."为了毕业而设计小说整理收集\r\n", "./ailaopo/".$bookname.".txt","w+");
for ($i=0;$i<$bookzj;$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("/(<title>)(.*?)(<\/title>)/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("</p>","\r\n",$content[2]));//用str_replace把$content[2]里有</p> 的换成\r\n
print_r($content);
exit;
//把第".($i+1)."节和标题与内容连接在一起放在变量
$result = " \r\n第".($i+1)."节--------".$title."_汪老师就是帅 --------- \r\n".$content;
writer($result, "./ailaopo/".$bookname.".txt","a+");//调用函数把$result
echo "小说".$bookname."共".$bookzj."节,现在整理到第".$i."节 _".$title."<br>";
}
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
}
?>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template