PHP 用正则表达式preg_match_all抓取天气预报

代码如下:

<?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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值