python php爬取小说,PHP实现的抓取小说网站内容功能示例

本文实例讲述了PHP实现的抓取小说网站内容功能。分享给大家供大家参考,具体如下:

爬取免费内容,弄到手机,听书,妥妥的。

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 = '/    (.*)
/';

//$content_grep = '/

(.*)
/sS';

$next_grep = '/

$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("
", '', $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];

}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php socket用法总结》、《php字符串(string)用法总结》、《PHP数学运算技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《PHP网络编程技巧总结》

希望本文所述对大家PHP程序设计有所帮助。

时间: 2019-06-27

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值