PHP教學 - 利用 curl 來抓取網頁 div 資料

突然想要來玩玩 curl 抓取網頁 div 資料,php 可以利用 curl 函式來執行,我們可以用來抓取別人網站的各種資料,本範例想抓取台銀的資料
<html>
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>抓取美金匯率</title>
<meta http-equiv="refresh" content="300"/>
</head>
<body>
<div style="margin-left:30px">
<?php
$url = "http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
preg_match_all('/<td class="decimal">([^<>]+)<\/td>/',$content,$target);
echo "<p>美金部分"."<br>";?>
<table width="100%" border="1">
<tr>
   <td><?php echo "即期匯率買入";?></td>
   <td><?php echo "即期匯率賣出";?></td>
</tr>
<tr>
<?php echo $target[0][2];
      echo $target[0][3];
?>
</tr>
</table></div></body></html>



留言

張貼留言

這個網誌中的熱門文章

c語言-關於#define用法

PHP教學 - 資料型態(Data Type) - 上

CMD常用網管指令