Home > php教程 > PHP源码 > PHP file_get_contents采集程序开发教程详解

PHP file_get_contents采集程序开发教程详解

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-08 17:23:50
Original
1424 people have browsed it

本文章来给各位同学介绍PHP file_get_contents采集程序开发教程详解,有需要了解的朋友可参考。

<script>ec(2);</script>


file_get_contents() 远程文件获取函数,用来获取远程页面内容
preg_match_all()进行全局正则表达式匹配,匹配多次,用于匹配列表
preg_match   ()进行正则表达式匹配,匹配一次,用于匹配终端
preg_replace ()进行正则表达式替换,用于过滤终端

具体步骤

Step 1 获取单页列表 和 单篇文章内容
在批量采集列表和内容之前,我们先将网站的单页列表和单篇文章的内容采集作为测试正则表达式对错。

列表页采集文章的链接地址:

 代码如下 复制代码

 //获取列表
 $url = '/s2005/shishi.shtml';
 $con=file_get_contents($url);
 //写正则获取列表中的文章链接
 /*范例 :           target='_blank'>湖南沅陵输电工程沉船事故共致6人死亡*/
 $preg = "|(.*)|iUs";
 // 正则中的/i表示 大小写不敏感 /U 非贪婪匹配 /s 点号可以匹配换行符
 preg_match_all($preg,$con,$arr);
 //var_dump($arr);
    /*
    array(3) {
  [0]=>
  array(40) {
    [0]=>
    string(126) " target='_blank'>甘肃河西走廊遭大风沙尘侵袭 瞬时最大风力9级"
    [1]=>
    string(112) " target='_blank'>一线城市住宅地价全部环比上涨"
    ... ...
    [39]=>
    string(124) " target='_blank'>湖南衡阳发生一起枪击案致1人死 警方正缉凶"
  }
  [1]=>
  array(40) {
    [0]=>
    string(46) "/20130418/n373180618.shtml"
    [1]=>
    string(46) "/20130418/n373180612.shtml"
    ... ...
    [39]=>
    string(46) "/20130418/n373161633.shtml"
  }
  [2]=>
  array(40) {
    [0]=>
    string(42) "甘肃河西走廊遭大风沙尘侵袭 瞬时最大风力9级"
    [1]=>
    string(28) "一线城市住宅地价全部环比上涨"
    ... ...
    [39]=>
    string(40) "湖南衡阳发生一起枪击案致1人死 警方正缉凶"
  }
}
    */
?>

单篇文章的采集:

 代码如下 复制代码

$url = 'http://www.111cn.net';
$con = file_get_contents($url);
//正则表达式分为标题和内容
$title_preg = "|

(.*)

|iUs";
$content_preg = "|(.*)|iUs";
preg_match($title_preg,$con,$title_arr);
preg_match($content_preg,$con,$content_arr);
?>
Related labels:
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template