原来的金价是采集http://www.kitco.cn/market/的,今天一看金价没有采集到,一看才发现他们网站改版了,现在的金价用js动态输出的,采集有点麻烦!

那就采集http://www.kitco.com/market/的吧,就是速度有点慢,以下是php代码,搞不定的也可以留言

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>黄金价格采集</title>
<style>
a {text-decoration: none;color:#800000;}
a:hover {text-decoration:underline;}
body{ font-family:Tahoma, "黑体", Arial; font-size:12px}
#moviediv{ margin:10px auto; width:600px; height:auto; padding:5px; border:1px solid #e0e0e0;}
#moviediv ul{ margin:0px ; padding:0px; list-style:none}
#moviediv li{background:#f5f5f5; text-align:left; margin:1px;}
#moviediv li b{ color:#FF0000; padding:5px}

#msg{ margin:10px auto; width:600px; height:auto; padding:5px; border:1px solid #e0e0e0;}
#msg ul{ margin:0px ; padding:0px; list-style:none}
#msg li{background:#f1f1f1; text-align:center; margin:1px; height:50px; line-height:50px}
/*input*/
.input{
height:18px;
line-height:18px;
border:solid 1px #ccc;
vertical-align:middle;
}
</style>
</head>

<body>
<div id="msg"><ul><li><img src="loadinfo.gif" />&nbsp;&nbsp;采集中,请稍等!</li></ul></div>
<?php
//汇率采集,采集百度的实时汇率
$ex_url = 'http://open.baidu.com/huilv/s?wd=1%C3%C0%D4%AA%B5%C8%D3%DA%B6%E0%C9%D9%C8%CB%C3%F1%B1%D2%D4%AA&tn=baiduhuilv';
//读取采集页面
$ex_content = @(file_get_contents($ex_url));
preg_match_all("/numFormat\((.*?)\);/is",$ex_content,$ex);
//print_r($tab);
//汇率
$ExchangeRate =$ex[1][2];
//金价采集
$url = 'http://www.kitco.com/market/';
//读取采集页面
$content = @(file_get_contents($url));
//如果file_get_contents 不行,试试下面的函数
//$ch = curl_init();
//$timeout = 10; // set to zero for no timeout
//curl_setopt ($ch, CURLOPT_URL, $url);
//curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//$content .= curl_exec($ch);
//echo $content;
//curl_close($ch);
preg_match_all("/High<\/td>(.*?)<\/table>/is",$content,$tab);
echo("<script>document.getElementById('msg').style.display='none';</script>");//隐藏加载图标
//print_r($tab);
//echo '<table>'.$tab[1][0].'</table>';
$getStr = $tab[1][0];
$getStrArr = array();
preg_match_all("/<td><p>(.*?)<\/td>/is",$getStr,$getStrArr);
//print_r($getStrArr);
//每克价格 = 美元*汇率/盎司 1美元= 6.6530 人民币元    1盎司=31.10348克
echo '今日金价<br>';
echo '日期:'.$getStrArr[1][0].'<br>';
echo '时间:'.$getStrArr[1][1].'<br>';
echo '美元:1美元= '.$ExchangeRate.' 人民币元<br>';
echo '黄金:'.round($getStrArr[1][3]    * $ExchangeRate / 31.10348,2).'<br>';
echo '白银:'.round($getStrArr[1][9]    * $ExchangeRate / 31.10348,2).'<br>';
echo '铂金:'.round($getStrArr[1][15] * $ExchangeRate / 31.10348,2).'<br>';
echo '钯金:'.round($getStrArr[1][21] * $ExchangeRate / 31.10348,2).'<br>';
?>
</body>
</html>

原创代码,转载请注明来自:http://www.cnblogs.com/kclteam/p/5278935.html

posted on 2016-03-15 12:32  kclteam  阅读(371)  评论(0编辑  收藏  举报