教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP数据采集程序采集天气网数据实例演示

PHP数据采集程序采集天气网数据实例演示

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP数据采集程序采集天气网数据实例演示等资源,欢迎您收藏本站,我们将为您提供最新的PHP数据采集程序采集天气网数据实例演示资源
本文章来给大家介绍利用PHP数据采集程序采集天气网数据实例,这个程序采集可以自动补全图片地址,获取内容中图片获取相关数据等等操作。


前言
我们在写一个Web程序的时候,总会想着把自己的网站更美观一些,功能能更多一些,有时候写一些小的工具或者加上小的插件会让我们的站点更加完善。比如万年历功能,比如我们现在要讲的天气预报功能。

当然我们没法利用专业的卫星接受数据,所以我们的天气数据来自现有的天气预报网站。利用天气预报网站提供的数据服务,我们可以写一个PHP爬虫,然后动态采集我们所需要的数据,并且在目标站点更新数据的时候,我们的程序也能做到同步更新,自动地获取数据。

下面就介绍一下如何编写一个简单的PHP数据采集程序(PHP爬虫)。

原理
给定一个网页的URL,使用PHP下载该网页并得到网页内容,然后通过正则表达式将其中我们感兴趣的数据提取出来,然后输出。

具体在这个例子中,我们要抓取的网页是 http://www.天气网com.cn/weather/101050101.shtml,我们感兴趣的是页面中的未来7天天气情况。

实现
0.获取天气预报网页的URL:

<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('copy4679')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4679>$url = "http://www.天气网com.cn/weather/101050101.shtml";
$page_content = file_get_contents($url);

在这里,file_get_contents() 函数会将 $url 指向的网页下载下来,并把网页内容作为一个字符串返回。于是,$page_content 变量中就是我们要抓取的网页的全部HTML代码了。接下来,我们要从其中抽取我们需要的数据。

1.使用正则表达式匹配符合条件的字符串
先输出 $page_content 的值,然后查看网页源代码,观察可知我们需要的字符串可以在

<!--day 1 -->
......
<!--day 7 -->

这两行的注释里找到。

使用正则表达式来取得 <!--day 1--> 和 <!--day 7--> 之间的所有内容:

<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('copy8125')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8125>

eregi("<!--day 1-->(.*)<!--day 7-->",$page_content,$res);

2.补全页面中图片的路径
由于远程网页中的图片路径都是像 /m2/i/icon_weather/29x20/d01.gif 这样的相对路径,我们需要把这些路径补全,在它们前面加上 http://www.天气网com.cn。

<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('copy3296')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3296>

$forecast = str_replace("<img src="","<img src="http://www.天气网com.cn",$res[0]);

至此,$forecast 中就是我们需要的天气预报信息了。这个简单的PHP爬虫也就写好了。

源代码
以下是这个抓取天气预报小程序的完整源代码,其中添加了一些测量各部分程序运行时间的代码,并可以通过设置 $start 和 $end 的值来控制抓取哪几天的信息。

<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('copy7793')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7793>


$url = "http://www.天气网com.cn/weather/101050101.shtml";
$t1 = time();
   
$page_content = file_get_contents($url);
$t2 = time();

$start = 1;
$end = 3;

if ($end > 7){
    echo "超出预报能力范围,请重新设置!";
}else {
    echo "未来".($end-$start)."天哈尔滨的天气预报("
              .date('Y-m-j')."发布)";

    eregi("--day $start--(.*)--day $end--", $page_content, $res);

    $forecast = str_replace("<img src="",
        "<img src="http://www.天气网com.cn", $res[0]);
    $t3 = time();

    echo $forecast;

    echo 'First step costs '.($t2 - $t1).' ms.';
    echo 'Last step costs '.($t3 - $t2).' ms.';
}

您可能感兴趣的文章:
PHP数据采集程序采集天气网数据实例演示
php采集程序
PHP调用百度天气接口API实现查询实时天气
PHP采集远程图片的实例代码
phpQuery采集网页内容的示例代码
python数据采集是什么
php采集cms有哪些
PHP内容采集器(PHP小偷程序)
PHP采集器的简单示例代码
PHP重启路由器以更换IP地址程序

[关闭]
~ ~