实时抓取YAHOO股票报价的代码
作者:bea
<? 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)){ $buf
<?
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)."
";
if (strstr($buffer, "rowspan=3"))
{
//echo "extract started at line #$lineCount
";
$startExtract = 1;
}
if ($startExtract && !$stopExtract)
{
if (strstr($buffer, "<a"))
{
$startPos = strrpos($buffer, "<");
$buffer = substr($buffer, $startPos);
}
//$text = trim(strip_tags($buffer));
//echo trim($buffer)."
";
$buffer = str_replace("
", " ", "$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
";
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)."
";
if (strstr($buffer, "rowspan=3"))
{
//echo "extract started at line #$lineCount
";
$startExtract = 1;
}
if ($startExtract && !$stopExtract)
{
if (strstr($buffer, "<a"))
{
$startPos = strrpos($buffer, "<");
$buffer = substr($buffer, $startPos);
}
//$text = trim(strip_tags($buffer));
//echo trim($buffer)."
";
$buffer = str_replace("
", " ", "$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
";
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]");
}
?>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 如何在WIN2K下安装PHP4.04
- 通过文字传递创建的图形按钮
- 计算2000年01月01日起到指定日的天数
- 文件上传程序的全部源码
- 一个简单计数器的源代码
- 一个用mysql_odbc和php写的serach数据库程序
- PHP脚本数据库功能详解(下)
- PHP脚本数据库功能详解(中)
- PHP脚本数据库功能详解(上)
- 屏蔽浏览器缓存另类方法
- 挑战最棒的留言本的源码(五)
- 使用网络地址转换实现多服务器负载均衡
- php抓即时股票信息
- 挑战最棒的留言本的源码(三)
- 利用PHP动态生成VRML网页
- 一个SQL管理员的web接口
- 挑战最棒的留言本的源码(二)
- 如何实现给定日期的若干天以后的日期
- 模拟SQLSERVER的两个函数:dateadd(),datediff()