>  기사  >  백엔드 개발  >  实时抓取YAHOO股票报价的代码_PHP教程

实时抓取YAHOO股票报价的代码_PHP教程

WBOY
WBOY원래의
2016-07-21 16:03:351204검색


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, "                {
                    $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, "
"))
            {
                 $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    {
        echo "$symbols[$i]
";
        getYahooQuote("$symbols[$i]");
    }
    ?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/316198.htmlTechArticle? function getYahooQuote($stockSymbol = CCR) { if (!$targetURL) $targetURL = http://finance.yahoo.com/q?s=$stockSymbolquot;; //设定要抓取的URL目标 $fd = fopen($targetURL, r)...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.