curl抓取網頁的問題
最近在用PHP寫一個可以抓取"樂透號碼"的程式
這是要抓取的網頁
http://lotto.arclink.com.tw/kj_01.html
但我一直無法抓取到全部的號碼 /_\
以下是我寫的code
<?php
$url = "http://lotto.arclink.com.tw/kj_01.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
preg_match('/<TD class="td_ball">(.+?)<\/TD>/',$content,$target);
preg_match('/<FONT color=blue>(.+?)<\/FONT>/',$content,$result);
echo $target[0];
echo $result[0];
?>
懇請大家替我解惑一下
是正規表示式寫得不好?
還是code本身需要改些什麼?
請問一下,
我將您的解答貼上之後有出現error
Parse error: syntax error, unexpected '@' in C:\AppServ\www\test.php on line 15
該如何解呢 /_\
已解決,真的非常感謝您!!!!!!!!!
1 個解答
- CYLv 51 0 年前最佳解答
preg_match 只抓一個 multiple matches 要用 preg_match_all
再注意一下return value 即可
部分code修改如下
preg_match_all('/<TD class="td_ball">(.+?)<\/TD>/',$content,$target);
preg_match('/<FONT color=blue>(.+?)<\/FONT>/',$content,$result);
for ($i=0; $i<6; $i++){ // 樂透頁還有出球順序 共12 matches
echo $target[1][$i]."<br>";
}
echo "<hr>";
echo $result[1];