PHP 用正则表达式preg_match_all抓取天气预报
2014-07-14 16:51
1566 查看
代码如下:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
//设定要抓取的页面(本例为北京天气)
$url = "http://www.weather.com.cn/html/weather/101010100.shtml";
$file = file_get_contents($url, false, $context);
preg_match_all('/<table class="yuBaoTable"[^>]*>(.*)<\/table>/isU',$file,$out1);
for($i = 0; $i < count($out1[1]); $i ++)
{
preg_match_all('/<tr[^>]*>(.*)<\/tr>/isU',$out1[1][$i],$out2);
preg_match_all('/<td[^>]*>\s*<a[^>]*>(.*)<\/a>\s*<\/td>/isU',$out2[1][0],$out3);
echo "【".date("Y-m")."-".$out3[1][0]."】<br />";
echo "白天:".$out3[1][2]." ".$out3[1][3]." ".$out3[1][4]." ".$out3[1][5]."<br />";
preg_match_all('/<td[^>]*>\s*<a[^>]*>(.*)<\/a>\s*<\/td>/isU',$out2[1][1],$out4);
if($out4[1][2])
{
echo "夜间:".$out4[1][2]." ".$out4[1][3]." ".$out4[1][4]." ".$out4[1][5]."<br />";
}
}
?>
运行效果:
http://1.xinyuan88.sinaapp.com/weather.php
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
//设定要抓取的页面(本例为北京天气)
$url = "http://www.weather.com.cn/html/weather/101010100.shtml";
$file = file_get_contents($url, false, $context);
preg_match_all('/<table class="yuBaoTable"[^>]*>(.*)<\/table>/isU',$file,$out1);
for($i = 0; $i < count($out1[1]); $i ++)
{
preg_match_all('/<tr[^>]*>(.*)<\/tr>/isU',$out1[1][$i],$out2);
preg_match_all('/<td[^>]*>\s*<a[^>]*>(.*)<\/a>\s*<\/td>/isU',$out2[1][0],$out3);
echo "【".date("Y-m")."-".$out3[1][0]."】<br />";
echo "白天:".$out3[1][2]." ".$out3[1][3]." ".$out3[1][4]." ".$out3[1][5]."<br />";
preg_match_all('/<td[^>]*>\s*<a[^>]*>(.*)<\/a>\s*<\/td>/isU',$out2[1][1],$out4);
if($out4[1][2])
{
echo "夜间:".$out4[1][2]." ".$out4[1][3]." ".$out4[1][4]." ".$out4[1][5]."<br />";
}
}
?>
运行效果:
http://1.xinyuan88.sinaapp.com/weather.php
相关文章推荐
- PHP与正则表达式 2 :一些修饰符与preg_match_all
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- PHP 正则表达式匹配函数 preg_match 与 preg_match_all
- PHP 正则表达式匹配函数 preg_match 与 preg_match_all
- PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
- (PHP)正则表达式-preg_match和preg_match_all()的用法
- PHP与正则表达式 2 :一些修饰符与preg_match_all
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- PHP 正则表达式匹配函数 preg_match 与 preg_match_all
- PHP preg_match正则表达式的使用
- PHP preg_match正则表达式详解
- PHP preg_match正则表达式详解
- PHP preg_match()进行正则表达式匹配
- preg_match_all正则表达式的基本使用
- 正则表达式之PHP篇match,match_all
- PHP preg_match正则表达式的使用
- php正则表达式验证 preg_match