[请益] 抓取网页资料

楼主: hardware (哈味)   2015-05-18 13:03:26
各位好
目前在抓网页的资料 一直抓不到想要的那个table
不晓得是我哪边的语法 有问题想问大家
我的目的是要抓取
http://www.cwb.gov.tw/V7/observe/real/46744.htm
05/18 12:45 31.4 晴 南 2.9 | 2 - - 71 1009.8 0.0
05/18 12:30 31.5 晴 南南西 3.0 | 2 - - 70 1009.8 0.0
05/18 12:15 31.9 晴 南南西 3.3 | 2 - - 69 1010.0 0.0
05/18 12:00 31.5 晴 南南西 2.5 | 2 - - 71 1010.1 0.0
这表格中的资料 但是一直抓不到
不晓得与法要怎么写
以下是我的code
抓到的都是表格外的东西 不晓得要怎么抓表格资料
有大大可以指点吗?
感恩
<meta http-equiv="refresh" content="" />
<?php
header(\"Content-Type: text/html; charset=utf-8");
function getWeather($city){
$toURL = \"http://www.cwb.gov.tw/V7/observe/real/$city.htm";
$post = array();
$ch = curl_init();
$options = array(
CURLOPT_REFERER=>'',
CURLOPT_URL=>$toURL,
CURLOPT_VERBOSE=>0,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_USERAGENT=>\"Mozilla/4.0 (compatible;)",
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query($post),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
//连接中央气象局
echo '<pre>';
preg_match_all('/<table class=\"BoxTable\"
[^>]*[^>]*>(.*)<\/div>/si',$result, $matches, PREG_SET_ORDER);
echo $result;
preg_match_all('/<td nowrap=\"nowrap\"
[^>]*[^>]*>(.*)<\/td>/si',$matches[0][1], $m1, PREG_SET_ORDER);
$m2 = explode('</td>',$m1[0][1]);
// print_r($m2);//取得每日资料m2[0~6]
$weather = array();
for($i=0;$i<=6;$i++){
preg_match_all('/src=[^>]*[^>](.*)/si',$m2[$i], $m5, PREG_SET_ORDER);//取得
天气图档
//echo $m5[0][0] . "<br />";
$m6 = explode('"',$m5[0][0]);
echo $m6[1] . "<br />";
$wi='http://www.cwb.gov.tw/V7/'.trim($m6[1],'\.\./\.\./');
$wtitle = $m6[3];
//print_r($wtitle);
$weather[$i]['date'] = date(\"Y-m-d", mktime(0, 0, 0, date(\"m"),
date(\"d")+$i,date(\"Y")));
$weather[$i]['temperature'] = trim(strip_tags($m2[$i]));
$weather[$i]['title'] = $wtitle;
$weather[$i]['img'] = $wi;
//一周天气图
$img = file_get_contents($weather[$i]['img']);
file_put_contents($i+1 .\".jpg",$img);
}
return($weather);
}
$weather=getWeather(\"46744") ;
print_r($weather);
echo $weather[0]['date'] . '<br />';
echo $weather[0]['temperature'];
echo $weather[2]['img'];
//打开写入
$filename = "../../ledweather.txt";
$file = fopen($filename, "w"); //以写模式打开文件
//fwrite($file, $weather[0]['date'] ."\r\n"); //换行\n
;window下要加 \r\n
fwrite($file, "温度 ");
fwrite($file, iconv('UTF-8','BIG5',$weather[0]['temperature']) . "°C\r\n");
fclose($file); //关闭文件
}
?>
作者: maymay36   2015-05-18 13:51:00
那页是用javascript loading的~资料网址是这个:http://goo.gl/s54dXw
楼主: hardware (哈味)   2015-05-18 14:57:00
谢谢
作者: iceblue7x (满城尽带路人甲)   2015-05-19 01:19:00
气象局有open data可以接啊 http://goo.gl/2Dr8zD
作者: poiuy999 (Nines)   2015-05-19 23:03:00
加油 之前做旅行社网站 抓Yahoo的

Links booklink

Contact Us: admin [ a t ] ucptt.com