程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 實時抓取YAHOO股票報價的代碼

實時抓取YAHOO股票報價的代碼

編輯:PHP綜合
<?
function getYahooQuote($stockSymbol = "CCR")
{
if (!$targetURL) $targetURL = "http://finance.yahoo.com/q?s=$stockSymbol&d=t"; //設定要抓取的URL目標     
        $fd = fopen("$targetURL", "r");
        $stopExtract = 0;  
        $startExtract = 0;  
        while (!feof($fd))  
        {
            $buffer = fgets($fd, 4096);
                //echo trim($buffer)."\n";  
            if (strstr($buffer, "rowspan=3"))
            {
                //echo "extract started at line #$lineCount\n";  
                $startExtract = 1;  
            }     
            if ($startExtract && !$stopExtract)     
            {

                if (strstr($buffer, "<a"))  
                {
                    $startPos = strrpos($buffer, "<");
                    $buffer = substr($buffer, $startPos);
                }
                //$text = trim(strip_tags($buffer));
                //echo trim($buffer)."\n";  

                $buffer = str_replace("\n\r", " ", "$buffer");
                if (strstr($buffer, "http://ichart.yahoo.com/v?s=$stockSymbol"))  
                {
                    //echo "ichart found!";
                    $stopExtract = 1;  
                }
                $capturedHTML .= $buffer;     

            }
            if ($startExtract && strstr($buffer, "<br>"))
            {
                 $stopExtract = 1;  
                //echo "extract stopped at line #$lineCount\n";          
                echo $capturedHTML;
                break;
            }
            $lineCount++;
        }
        fclose($fd);
    }

    //以下為抓取的一個例子
    $symbols = array('CCR', 'IIXL','SAPE','WBVN' );
    $symbolCount = count($symbols);
    for ($i=0; $i< $symbolCount; $i++)
    {
        echo "$symbols[$i]<br>";
        getYahooQuote("$symbols[$i]");
    }
    ?>


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved