正则采集问题,求解,在线等

php中文网
发布: 2016-06-23 14:24:25
原创
1152人浏览过

目标页:http://www.weather.com.cn/weather/101070101.shtml

查看其网页源码,想得到425行的数据,即“今天是2013年10月16日 星期三 农历九月十二”和621行“沈阳天气预报 (2013-10-16 18:00发布)" 和641行~765行之间主要数据。用图表示一下,见下图:


另外我自己写的一条规则,试着取了下这样的数据"沈阳天气预报 (2013-10-16 
          11:00发布)"但输入后为空数组,求指导!多谢!


回复讨论(解决方案)

1

$s=file_get_contents('http://www.weather.com.cn/weather/101070101.shtml');preg_match('/<h1 class="weatheH1"  id="live">\s+(.+)\s+<\!--today5-->/s',$s,$m);echo $m[1];
登录后复制


2
 preg_match('/<div class="weatherYubao"  id="7d">\s+<h1 class="weatheH1">\s+(.+?)\s+<span>/s',$s,$m);echo $m[1];
登录后复制


3 不知道你想要得到什么格式的数据。

一直都感觉正则好高端  

1

$s=file_get_contents('http://www.weather.com.cn/weather/101070101.shtml');preg_match('/<h1 class="weatheH1"  id="live">\s+(.+)\s+<\!--today5-->/s',$s,$m);echo $m[1];
登录后复制


2
 preg_match('/<div class="weatherYubao"  id="7d">\s+<h1 class="weatheH1">\s+(.+?)\s+<span>/s',$s,$m);echo $m[1];
登录后复制


3 不知道你想要得到什么格式的数据。

就是按日期,顺序取到各项值就行,文本就行,比如:星期四 白天晴 高温16摄氏度 西南风微风 夜间晴 低温3摄氏度 西南风微风(换行)...如此把这块数据都取出来

如下源码:

<!--day 1-->          <table class="yuBaoTable" width="100%" border="0" cellspacing="0" cellpadding="0">          <tr>            <td width="15%" rowspan="2" class="t0" style="background:#f4f7fc;"><a title="农历九月十二">16日星期三</a></td>            <td width="12%" style="background:#f4f7fc;">白天</td>            <td width="10%">              <a href="http://www.weather.com.cn/static/html/legend.shtml" target="_blank">                <img  src="/m2/i/icon_weather/29x20/d00.gif" / alt="正则采集问题,求解,在线等" >              </a>            </td>            <td width="18%"><a href ="http://baike.weather.com.cn/index.php?doc-view-767.php" target="_blank">晴</a></td>            <td width="15%">              <a href="http://baike.weather.com.cn/index.php?doc-view-1148.php" target="_blank">                <b>高温 <strong>12℃</strong></b>              </a>            </td>            <td width="15%">              <a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">                东北风              </a>            </td>            <td width="15%">              <a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">                微风              </a>            </td>          </tr>          <tr>            <td   style="max-width:90%">夜间</td>            <td>              <a href="http://www.weather.com.cn/static/html/legend.shtml" target="_blank">                <img  src="/m2/i/icon_weather/29x20/n00.gif" / alt="正则采集问题,求解,在线等" >              </a>            </td>            <td><a href ="http://baike.weather.com.cn/index.php?doc-view-767.php" target="_blank">晴</a></td>            <td>              <a href="http://baike.weather.com.cn/index.php?doc-view-1386.php" target="_blank">                <span>低温 <strong>-2℃</strong></span>              </a>            </td>            <td>              <a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">                东北风              </a>            </td>            <td>              <a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">                微风              </a>            </td>          </tr>          </table>          <!--day 2-->
登录后复制

只想要(取得3天数据):16日星期三 白天:晴 12℃ 东北风微风 夜间:晴 -2℃ 东北风微风

另外发现一个小问题(见下图),不知道应该怎样处理一下?

另外发现一个小问题(见下图),不知道应该怎样处理一下?

echo preg_replace('/ \s+/s','',$result[1]);

3

preg_match_all('/<table class="yuBaoTable" width="100%" border="0" cellspacing="0" cellpadding="0">(.+)<\/table>/isU',$s,$m); echo join('<br>',array_map('strip_tags',array_slice($m[1],0,3)));
登录后复制

哦,写错了。第一个应该是 echo preg_replace('/ \s+/s','',$result2[1]);

问题3
这两行执行后,还有个小缺陷,有很多不必要的换行存在,能弄掉吗?(查看源代码如下)

今天是2013年10月17日 星期四 农历九月十三  沈阳天气预报 (2013-10-1708:00发布)                      17日星期四            白天                                                                                晴                                          高温 16℃                                                                    西南风                                                                    微风                                                          夜间                                                                                晴                                          低温 3℃                                                                    西南风                                                                    微风                                              <br>                      18日星期五            白天                                                                                晴                                          高温 18℃                                                                    西南风                                                                    3-4级                                                          夜间                                                                                多云                                          低温 6℃                                                                    西南风                                                                    3-4级                                              <br>                      19日星期六            白天                                                                                阵雨                                          高温 15℃                                                                    西南风                                                                    3-4级                                                          夜间                                                                                多云                                          低温 4℃                                                                    西南风                                                                    微风                                              
登录后复制

 echo preg_replace('/\s+/s','',join('
',array_map('strip_tags',array_slice($m[1],0,3))));

 echo preg_replace('/\s+/s','',join('
',array_map('strip_tags',array_slice($m[1],0,3))));

换行是去掉了,但全边一起了

沈阳天气预报 (2013-10-1711:00发布)17日星期四白天晴高温16℃西南风微风夜间晴低温3℃西南风微风<br>18日星期五白天晴高温18℃西南风3-4级夜间多云低温6℃西南风3-4级<br>19日星期六白天阵雨高温15℃西南风3-4级夜间多云低温4℃西南风微风
登录后复制

有办法让换行变成一个空格吗(现有的
保留)?

要是不好实现就算了,这样也可以了。说什么好呢?太感谢了

echo preg_replace('/\s+/s',' ',join('
',array_map('strip_tags',array_slice($m[1],0,3)))); 

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号