目标页: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发布)"但输入后为空数组,求指导!多谢!
另外我自己写的一条规则,试着取了下这样的数据"沈阳天气预报 (2013-10-16
11:00发布)"但输入后为空数组,求指导!多谢!
$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 不知道你想要得到什么格式的数据。
<!--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" />
</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="background:#f4f7fc;">夜间</td>
<td>
<a href="http://www.weather.com.cn/static/html/legend.shtml" target="_blank">
<img src="/m2/i/icon_weather/29x20/n00.gif" />
</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℃ 东北风微风
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)));
今天是2013年10月17日 星期四 农历九月十三
沈阳天气预报 (2013-10-1708:00发布)
17日星期四
白天
晴
高温 16℃
西南风
微风
夜间
晴
低温 3℃
西南风
微风
<br>
18日星期五
白天
晴
高温 18℃
西南风
3-4级
夜间
多云
低温 6℃
西南风
3-4级
<br>
19日星期六
白天
阵雨
高温 15℃
西南风
3-4级
夜间
多云
低温 4℃
西南风
微风
换行是去掉了,但全边一起了
沈阳天气预报 (2013-10-1711:00发布)17日星期四白天晴高温16℃西南风微风夜间晴低温3℃西南风微风<br>18日星期五白天晴高温18℃西南风3-4级夜间多云低温6℃西南风3-4级<br>19日星期六白天阵雨高温15℃西南风3-4级夜间多云低温4℃西南风微风
有办法让换行变成一个空格吗(现有的<br>保留)?