简单采集了yahoo的一些数据
以前在公司就写过类似的东西,这次是帮以前的上司写了一个简单的采集程序。
很简单的。。汗。没什么技术含量的。
数据来源:http://cn.finance.yahoo.co...
演示地址:http://traffic02.100steps....
(修改了下,增加了数据缓存功能。。汗,没有使用lite_cache了,自己写了个最简单的那种。。)
<?
set_time_limit(0);
$max_time=3600;
$cache_file='cache_yahoo.txt';
$nowtime=time();
if(!file_exists($cache_file)){
$filetime=0;
}else{
$filetime=filemtime($cache_file);
}
if($filetime+$max_time<$nowtime){
//更新
ob_start();
function get_yahoo_info($url){
$content=file_get_contents($url);
$pattern='|<tr class="yfnc_modtitle1"><td><small><b>(.*)</b> \((.*)\) (.*)</small></td><td align="right">|U';
preg_match_all($pattern,$content,$out);
$info['company_name']=$out[1][0];
$info['company_stock']=$out[2][0];
$pattern='|<td class="yfnc_tablehead1" width="52%">最近交易价:</td><td class="yfnc_tabledata1"><big><b>(.*)</b></big></td>|U';
preg_match_all($pattern,$content,$out);
$info['stock_price']=$out[1][0];
$pattern='|<td class="yfnc_tablehead1" width="52%">涨跌:</td><td class="yfnc_tabledata1">(.*)</td>|U';
preg_match_all($pattern,$content,$out);
$info['stock_upordown']=$out[1][0];
$pattern='|<td class="yfnc_tablehead1" width="54%">市值:</td><td class="yfnc_tabledata1">(.*)</td>|U';
preg_match_all($pattern,$content,$out);
$info['stock_value']=$out[1][0];
return $info;
}
$urls=explode(',',file_get_contents('url.txt'));
$i=0;
$matchs=array();
foreach($urls as $url){
$rs=get_yahoo_info($url);
if(!empty($rs)){
$matchs[$i]=$rs;
$i++;
$rs='';
}
}
//print_r(get_yahoo_info('http://cn.finance.yahoo.com/q?s=000063.SZ'));
?>
<table border="1" width="100%" cellpadding="6" style="border-collapse: collapse" bordercolor="#8CBFF9" cellspacing="0" bgcolor="#DFEDFD">
<tr>
<td width="30" height="" valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" > </td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">公司名称</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">股票代号</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">股票价格</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">股票涨幅</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">市值</div></td>
</tr>
<?
$i=1;
foreach($matchs as $match){
?>
<tr>
<td height="" align="center" valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><?=$i?></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['company_name']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['company_stock']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['stock_price']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['stock_upordown']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['stock_value']?></div></td>
</tr>
<?
$i++;
}?>
</table>
<?
$content=ob_get_clean();
$fp=fopen($cache_file,'w');
if(!$fp)echo '打开文件失败';
if (flock($fp, LOCK_EX)) { // 进行排它型锁定
if(!fwrite($fp, $content))echo '文件写入失败';
flock($fp, LOCK_UN); // 释放锁定
} else {
//echo "Couldn't lock the file !";
if(!fwrite($fp, $content))echo '文件写入失败';
}
fclose($fp);
//echo 'cache time:'.time();
echo $content;
}else{
//echo 'cache!';
echo file_get_contents($cache_file);
}
?>
很简单的。。汗。没什么技术含量的。
数据来源:http://cn.finance.yahoo.co...
演示地址:http://traffic02.100steps....
(修改了下,增加了数据缓存功能。。汗,没有使用lite_cache了,自己写了个最简单的那种。。)
复制代码 代码如下:
<?
set_time_limit(0);
$max_time=3600;
$cache_file='cache_yahoo.txt';
$nowtime=time();
if(!file_exists($cache_file)){
$filetime=0;
}else{
$filetime=filemtime($cache_file);
}
if($filetime+$max_time<$nowtime){
//更新
ob_start();
function get_yahoo_info($url){
$content=file_get_contents($url);
$pattern='|<tr class="yfnc_modtitle1"><td><small><b>(.*)</b> \((.*)\) (.*)</small></td><td align="right">|U';
preg_match_all($pattern,$content,$out);
$info['company_name']=$out[1][0];
$info['company_stock']=$out[2][0];
$pattern='|<td class="yfnc_tablehead1" width="52%">最近交易价:</td><td class="yfnc_tabledata1"><big><b>(.*)</b></big></td>|U';
preg_match_all($pattern,$content,$out);
$info['stock_price']=$out[1][0];
$pattern='|<td class="yfnc_tablehead1" width="52%">涨跌:</td><td class="yfnc_tabledata1">(.*)</td>|U';
preg_match_all($pattern,$content,$out);
$info['stock_upordown']=$out[1][0];
$pattern='|<td class="yfnc_tablehead1" width="54%">市值:</td><td class="yfnc_tabledata1">(.*)</td>|U';
preg_match_all($pattern,$content,$out);
$info['stock_value']=$out[1][0];
return $info;
}
$urls=explode(',',file_get_contents('url.txt'));
$i=0;
$matchs=array();
foreach($urls as $url){
$rs=get_yahoo_info($url);
if(!empty($rs)){
$matchs[$i]=$rs;
$i++;
$rs='';
}
}
//print_r(get_yahoo_info('http://cn.finance.yahoo.com/q?s=000063.SZ'));
?>
<table border="1" width="100%" cellpadding="6" style="border-collapse: collapse" bordercolor="#8CBFF9" cellspacing="0" bgcolor="#DFEDFD">
<tr>
<td width="30" height="" valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" > </td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">公司名称</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">股票代号</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">股票价格</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">股票涨幅</div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#E1EEFD" ><div align="center">市值</div></td>
</tr>
<?
$i=1;
foreach($matchs as $match){
?>
<tr>
<td height="" align="center" valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><?=$i?></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['company_name']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['company_stock']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['stock_price']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['stock_upordown']?></div></td>
<td valign="top" bordercolor="#ABD0FA" bgcolor="#FFFFFF" ><div align="center"><?=$match['stock_value']?></div></td>
</tr>
<?
$i++;
}?>
</table>
<?
$content=ob_get_clean();
$fp=fopen($cache_file,'w');
if(!$fp)echo '打开文件失败';
if (flock($fp, LOCK_EX)) { // 进行排它型锁定
if(!fwrite($fp, $content))echo '文件写入失败';
flock($fp, LOCK_UN); // 释放锁定
} else {
//echo "Couldn't lock the file !";
if(!fwrite($fp, $content))echo '文件写入失败';
}
fclose($fp);
//echo 'cache time:'.time();
echo $content;
}else{
//echo 'cache!';
echo file_get_contents($cache_file);
}
?>