PHP 用正则表达式preg_match_all抓取天气预报
来源:互联网 发布:python运维脚本交换机 编辑:程序博客网 时间:2024/04/30 12:53
代码如下:
<?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
0 0
- PHP 用正则表达式preg_match_all抓取天气预报
- PHP与正则表达式 2 :一些修饰符与preg_match_all
- PHP函数preg_match_all正则表达式的简单应用
- PHP函数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_all函数正则匹配详解
- PHP 正则表达式抓取网页内容。
- php preg_match_all preg_match 和正规表达式
- 正则抓取SINA天气预报数据!!!
- [30期] PHP正则表达式写天气预报小偷程序
- [30期] PHP正则表达式写天气预报小偷程序
- [30期] PHP正则表达式写天气预报小偷程序
- PHP抓取天气预报的代码
- 网页抓取信息(php正则表达式、php操作excel)
- preg_match_all 正则表达式贪婪与非贪婪模式
- android binder 机制二(client和普通server)
- OLE DB 访问接口 "SQLNCLI" 返回了消息
- 中缀转后缀求值
- happy every moment
- DB2表管理的语句
- PHP 用正则表达式preg_match_all抓取天气预报
- unicode转码
- IOS的UIView
- 大数计算器
- C#获取从指定的文件路径,获取该文件的图标,然后显示在image里
- DES加密解密(配合GTMBase64)
- 作为成冬青的曾经
- 浅谈工业机器人的运动停止
- mysql 常用命令之函数