教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP file_get_contents采集程序开发教程详解

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

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP file,get,contents采集程序开发教程详解等资源,欢迎您收藏本站,我们将为您提供最新的PHP file,get,contents采集程序开发教程详解资源
本文章来给各位同学介绍PHP file_get_contents采集程序开发教程详解,有需要了解的朋友可参考。


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

具体步骤

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

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3385')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3385>

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

单篇文章的采集:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8438')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8438>

<?php
$url = 'http://www.jiaochengji.com';
$con = file_get_contents($url);
//正则表达式分为标题和内容
$title_preg = "|<h1>(.*)</h1>|iUs";
$content_preg = "|<!-- 正文 -->(.*)<!-- 分享 -->|iUs";
preg_match($title_preg,$con,$title_arr);
preg_match($content_preg,$con,$content_arr);
?>

您可能感兴趣的文章:
PHP file_get_contents采集程序开发教程详解
php 获取远程网页内容简单函数
php读取远程文件的三种方法分享
PHP内容采集器(PHP小偷程序)
php采集程序
file_get_contents只读取网页的部分内容
php file_get_contents函数抓取页面信息的代码
php不能开发安卓app吗?
php中curl、fsocket、file_get_content函数比较
php可以开发小程序吗?

[关闭]
~ ~