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

yipeiwu_com6年前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 = '/    (.*)<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];
}

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

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

相关文章

用php来限制每个ip每天浏览页面数量的实现思路

实现思路:首先,创建一个表,比如下面的 复制代码 代码如下:   CREATE TABLE ip_log   (   ip_log_ip VARCHAR(40),   ip_log_da...

探讨php中header的用法详解

 header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more info...

php进程间通讯实例分析

本文实例讲述了php进程间通讯的方法。分享给大家供大家参考,具体如下: php单进程单线程处理批量任务太慢了,受不鸟了,但是php不能多线程,最终选择了多进程处理批量任务. php多进程...

php编写的抽奖程序中奖概率算法

们先完成后台PHP的流程,PHP的主要工作是负责配置奖项及对应的中奖概率,当前端页面点击翻动某个方块时会想后台PHP发送ajax请求,那么后台PHP根据配置的概率,通过概率算法给出中奖结...

PHP版国家代码、缩写查询函数代码

复制代码 代码如下: <?php function transCountryCode($code) { $index=array('AA'=>'阿鲁巴', 'AD'=>...