这篇文章主要介绍了PHP实现的抓取小说网站内容功能,涉及php页面抓取、正则匹配、文件读写等相关操作技巧,需要的朋友可以参考下
本文实例讲述了PHP实现的抓取小说网站内容功能。分享给大家供大家参考,具体如下:
php0.jpg

ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)');
ini_set('max_execution_time', '0');
$base = 'https://www.qu.la/book/19434/';
$start = '7504808.html';
$content_grep = '/    (.*)<br\/>/';
//$content_grep = '/<div id="content">(.*)<br\/>/sS';
$next_grep = '/<a id="pager_next" href=\"(\d+\.html)\" target="_top" class="next">下一章<\/a>/';
$next = $start;
$file_name = '听书了.txt';
while($next) {
echo 'getting ' . $next . PHP_EOL;
$result = file_get_contents($base . $next);
preg_match_all($content_grep, $result, $match);
$isTitle = true;
$content = "";
foreach($match[1] as $line) {
$line  = str_replace("<br/>", '', $line);
$line  = str_replace(" ", '', $line);
if($isTitle) {
$content = $line . PHP_EOL . PHP_EOL;
$isTitle = false;
} else {
$content .= '    ' . $line . PHP_EOL . PHP_EOL;
}
}
$file = fopen($file_name, 'a');
echo 'write length: ' . strlen($content) . PHP_EOL;
fwrite($file, $content);
fclose($file);
echo '.';
preg_match($next_grep, $result, $match);
$next = $match[1];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持轩宇网。

滇ICP备16003699号-2
最后修改:2019 年 06 月 27 日
如果觉得我的文章对你有用,请随意赞赏