• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

简单采集了yahoo的一些数据

php 搞代码 4年前 (2022-01-01) 30次浏览 已收录 0个评论
以前在公司就写过类似的东西,这次是帮以前的上司写了一个简单的采集程序。

    很简单的。。汗。没什么技术含量的。
    数据来源: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=’|

(.*) \((.*)\)  (.*)|U’; 
    preg_match_all($pattern,$content,$out); 
    $info[‘company_name’]=$out[1][0]; 
    $info[‘company_stock’]=$out[2][0]; 

    $pattern=’|

最近交易价:(.*)

|U’; 
    preg_match_all($pattern,$content,$out); 
    $info[‘stock_price’]=$out[1][0]; 

    $pattern=’|

涨跌:(.*)

|U’; 
    preg_match_all($pattern,$content,$out); 
    $info[‘stock_upordown’]=$out[1][0]; 

    $pattern=’|

市值:(.*)

|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&#8217;)); 
?> 

 
  

 
    

 
    

 
    

 
    

 
    

 
    

 
  

  来源gaodai#ma#com搞*代#码网
  <?  
  $i=1; 
foreach($matchs as $match){ 
  ?> 
  

 
    

 
    

 
    

 
    

 
    

 
    

 
  

 
  <?  
  $i++; 
  }?> 

 
公司名称
股票代号
股票价格
股票涨幅
市值

 
<?  
    $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); 

?> 

以上就是简单采集了yahoo的一些数据的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:简单采集了yahoo的一些数据

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址