PHP 簡易蜘蛛(spider)~同步發表在[104開發者俱樂部中]
同步發表在[104開發者俱樂部中]:http://plus.104.com.tw/activity/0dd1bfd9-643c-4edc-ac88-f424ec203c1c
執行結果:http://jashliao.eu/tools/save_stock/
GITHUB:https://github.com/jash-git/TW_save_stock
透過PHP的file函數抓取YAHOO的對應股票資訊,之後preg_match函數分析出股價並顯示在畫面上,最後利用JS(javascript)定時(Timer)刷新[window.location.reload();]頁面,達到『簡易蜘蛛(spider)』
在2017/10/07把程式改成讀取CSV檔
<HTML>
<HEAD>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf8″>
<script language=Javascript>
function RunTimer(){
setTimeout(“Timer01();”, 60000);
}
function Timer01(){
window.location.reload();
}
</script>
</HEAD>
<Body onload=RunTimer();>
<?php
header(“Content-Type:text/html; charset=utf-8”);
set_time_limit(0);//無限等待
echo ‘<B>jash-liao 存股-股價即時更新系統:</B><br><br>’;
echo ‘<br><br><br><br><br><br>’;
date_default_timezone_set(“Asia/Taipei”);
echo ‘更新時間:’. date (“Y- m – d / H : i : s”);
$time=’&t=’.date (“YmdHis”);
echo “<font size=’26’ face=’Arial’>”;//PHP放大字體
echo ‘<table style=”font-size:22px; font-family:Arial; width:500px; border: 1px solid black; border-collapse: collapse;”>’;
$fp = fopen(“stock.csv”, “r”);
while (($data = fgetcsv($fp, 1000, “,”)) !== FALSE)
{
$id=$data[1];
$name=$data[0];
$text=file(‘https://tw.stock.yahoo.com/q/q?s=’.$id.$time);
foreach ($text as $line_num => $line) {
//echo “Line #<b>{$line_num}</b> : ” . htmlspecialchars($line) . “<br />\n”;
if (preg_match(“/><b>/i”, $line))
{
echo ‘<tr style=”border: 1px solid black; border-collapse: collapse;”>’;
echo ‘<td>’.$name.'[ ‘.$id.’ ]</td>’;
echo $line;
echo ‘</tr>’;
}
}
}
fclose($fp);
echo ‘</table>’;
echo ‘<br>’;
echo ‘資料來源:每分鐘跟YAHOO要一次’;
//print_r($text);
?>
</Body>
<HTML>
|