实时抓取YAHOO股票报价的代码
程序员文章站
2022-03-04 16:04:40
function getyahooquote($stocksymbol = "ccr") { if (!$targeturl) $targe...
<?
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]");
}
?>
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]");
}
?>
上一篇: 基于mysql的bbs设计(五)
下一篇: 简单理解同步与异步