实时抓取YAHOO股票报价的php代码
时间:2007-02-17 来源:PHP爱好者
<?
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
function getYahooQuote($stockSymbol = "CCR")
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (!$targetURL) $targetURL = "http://finance.yahoo.com/q?s=$stockSymbol&d=t"; //设定要抓取的URL目标
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$fd = fopen("$targetURL", "r");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$stopExtract = 0;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$startExtract = 0;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
while (!feof($fd))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$buffer = fgets($fd, 4096);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo trim($buffer)."n";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (strstr($buffer, "rowspan=3"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo "extract started at line #$lineCountn";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$startExtract = 1;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if ($startExtract && !$stopExtract)
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (strstr($buffer, "<a"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$startPos = strrpos($buffer, "<");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$buffer = substr($buffer, $startPos);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//$text = trim(strip_tags($buffer));
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo trim($buffer)."n";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$buffer = str_replace("nr", " ", "$buffer");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (strstr($buffer, "http://ichart.yahoo.com/v?s=$stockSymbol"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo "ichart found!";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$stopExtract = 1;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$capturedHTML .= $buffer;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if ($startExtract && strstr($buffer, "<br>"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$stopExtract = 1;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo "extract stopped at line #$lineCountn";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
echo $capturedHTML;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
break;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$lineCount++;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
fclose($fd);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//以下为抓取的一个例子
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$symbols = array('CCR', 'IIXL','SAPE','WBVN' );
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$symbolCount = count($symbols);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
for ($i=0; $i<$symbolCount; $i++)
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
echo "$symbols[$i]<br>";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
getYahooQuote("$symbols[$i]");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
?>
php爱好者站 http://www.phpfans.net php动态|php基础|php进阶|php模板.
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
function getYahooQuote($stockSymbol = "CCR")
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (!$targetURL) $targetURL = "http://finance.yahoo.com/q?s=$stockSymbol&d=t"; //设定要抓取的URL目标
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$fd = fopen("$targetURL", "r");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$stopExtract = 0;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$startExtract = 0;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
while (!feof($fd))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$buffer = fgets($fd, 4096);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo trim($buffer)."n";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (strstr($buffer, "rowspan=3"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo "extract started at line #$lineCountn";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$startExtract = 1;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if ($startExtract && !$stopExtract)
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (strstr($buffer, "<a"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$startPos = strrpos($buffer, "<");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$buffer = substr($buffer, $startPos);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//$text = trim(strip_tags($buffer));
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo trim($buffer)."n";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$buffer = str_replace("nr", " ", "$buffer");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if (strstr($buffer, "http://ichart.yahoo.com/v?s=$stockSymbol"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo "ichart found!";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$stopExtract = 1;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$capturedHTML .= $buffer;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
if ($startExtract && strstr($buffer, "<br>"))
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$stopExtract = 1;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//echo "extract stopped at line #$lineCountn";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
echo $capturedHTML;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
break;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$lineCount++;
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
fclose($fd);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
//以下为抓取的一个例子
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$symbols = array('CCR', 'IIXL','SAPE','WBVN' );
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
$symbolCount = count($symbols);
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
for ($i=0; $i<$symbolCount; $i++)
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
{
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
echo "$symbols[$i]<br>";
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
getYahooQuote("$symbols[$i]");
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
}
chi na it p owe r .comicYSX1sCYagVdgfzEygV5Oguc
?>
php爱好者站 http://www.phpfans.net php动态|php基础|php进阶|php模板.
相关阅读 更多 +