抓取sina读书频道的小说
时间:2009-08-07 来源:hkebao
<?php
/*配置数组*/
$arrayContent = array(
0=>40438, //小说id
1=>27128, //章节起始id
2=>27204, //章节结束id
3=>'浴火凤凰' //小说名字
);
/*内容规则*/
$titlePre = "/<h1>(.*?)<\/h1>/"; //标题
$contentsPre = "/<div id=\"contTxt\" class=\"contTxt1\">(.*?)<\/div>/"; //内容
/*写内容*/
for( $i = $arrayContent[1]; $i<=$arrayContent[2]; $i++)
{
$url = "http://vip.book.sina.com.cn/book/chapter_{$arrayContent[0]}_{$i}.html";
$cont = file_get_contents($url);
preg_match_all($titlePre,$cont,$title);
preg_match_all($contentsPre,$cont,$contents);
$fh = fopen($arrayContent[3].".txt",'a+');
$writeContents = $title[1][0]."\r\n".str_replace('</p>',"\r\n",str_replace('<p>',' ',$contents[1][0]))."\r\n";
if( fwrite($fh,$writeContents) )
{
echo "第{$i}页写入成功-";
}
fclose($fh);
}
?>
晚上闲着没事干,弄几本小说放手机里看,上面是代码。