Home php教程 PHP源码 一个简单PHP采集器

一个简单PHP采集器

May 25, 2016 pm 05:15 PM

PHP代码

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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)