[請益] 抓取網頁資料

看板PHP作者 (硬體)時間9年前 (2015/05/18 13:03), 編輯推噓2(203)
留言5則, 4人參與, 最新討論串8/8 (看更多)
各位好 目前在抓網頁的資料 一直抓不到想要的那個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); //關閉文件 } ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.89.71 ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1431925409.A.724.html

05/18 13:51, , 1F
那頁是用javascript loading的~
05/18 13:51, 1F

05/18 13:56, , 2F
資料網址是這個:http://goo.gl/s54dXw
05/18 13:56, 2F

05/18 14:57, , 3F
謝謝
05/18 14:57, 3F

05/19 01:19, , 4F
氣象局有open data可以接啊 http://goo.gl/2Dr8zD
05/19 01:19, 4F

05/19 23:03, , 5F
加油 之前做旅行社網站 抓Yahoo的
05/19 23:03, 5F
文章代碼(AID): #1LMNAXSa (PHP)
討論串 (同標題文章)
文章代碼(AID): #1LMNAXSa (PHP)