富國基金凈值
最近用螞蟻聚寶的慧定投買了富國中證500指數(shù)增強(qiáng)這只基金,想要計算下每周扣款之后能拿到多少份額。所以寫了個計算頁面,目標(biāo)是用PHP爬取富國基金公司官網(wǎng)上的富國中證500指數(shù)增強(qiáng)這只基金的當(dāng)天凈值,然后按照實際扣款金額計算出所得份額。
- 實際代碼如下:
<?php
//抓取整個頁面
$url="http://www.fullgoal.com.cn/";
$contents = file_get_contents($url);
//依據(jù)抓取 到的頁面按照table進(jìn)行過濾
$preg1='/<table[^>]*>(.*?) <\/table>/si';
$res2="";
preg_match_all($preg1,$contents,$res2);
$res3=$res2[0][9];
//echo $res3;
//將過濾后的table再次按照td進(jìn)行過濾
$res4="";
preg_match_all('/<td[^>]*>(.*?) <\/td>/si',$res3,$res4);
$res5=$res4[0][0];
//將給字符串按照空格拆分成數(shù)組
$res6=explode(" ",$res5);
//得到最終的基金凈值和時間
$funToday=$res6[27];
$funValueToday=$res6[45];
//對數(shù)據(jù)進(jìn)行去空格和去html標(biāo)簽操作
$fund1=trim($funValueToday);
$fund2=strip_tags($fund1);
?>
<html>
<head>
<meta charset="utf-8" />
<title>慧定投計算器</title>
<style type="text/css">
/*全局樣式設(shè)置*/
*{
margin:0px;
padding:0px;
}
body {
width:600px;
margin:10px auto;
font-size:14px;
}
input:hover {
border:1px solid #363636;
box-shadow: 2px 3px 2px #888888;
}
span {
padding:20px;
text-align: left;
}
/*局部設(shè)置*/
.div1 {
width: 600px;
margin: 0px auto;
text-align: center;
padding-top: 10px;
}
.divP p {
padding-top: 10px;
width:300px;
font-size:20px;
font-style: inherit;
text-align: center;
margin:0px auto;
}
.divP h2{
color: rgb(218,88,77);
font-size: 2em;
}
.titlename {
color: rgb(215,88,77);
}
input {
width: 250px;
height: 30px;
margin:5px auto;
text-align: left;
}
.subM {
font-weight: bold;
font-size: 22px;
width: 200px;
height: 30px;
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="div1">
<div class="img1">
<img src="http://upload-images.jianshu.io/upload_images/4167376-d6f538037602f6cd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /><br />
</div>
<form action="huidingtou-fuguo.php" method="post" class="form1">
<div class="divP">
<p class="titlename"><h2>慧定投計算器</h2></p>
<p><h3>富國中證500指數(shù)增強(qiáng)</h3></p>
<p><?php echo "凈值日期:".$funToday;?></p>
<p><strong>慧定投實際扣款額:</strong></p><input type="text" name="ShijikouK" class="zongFe"value= <?php echo @$_POST['ShijikouK'];?>>><br />
<input type="submit" value="計 算" class="subM" />
</div>
</form>
</div>
</body>
</html>
<?php
//抓取頁面輸入的數(shù)據(jù)
//實際扣款額
$ShijikouK=$_REQUEST['ShijikouK'];
//計算凈申購額
$jingshenE=$ShijikouK/(1+0.0015);
//計算基金份額
$fundShare=$jingshenE/$fund2;
echo "<p style='font-size: 20px;text-align:center;padding-top:15px;'><strong>當(dāng)天凈值:</strong>".$fund2."元/份</p><p style='font-size: 20px;text-align:center;padding-top:15px;'><strong>凈申購額:</strong>".round($jingshenE,2)."元</p><p style='font-size: 20px;text-align:center;padding-top:15px;'><strong>基金份額:</strong>".round($fundShare,2)."份</p>";
?>
最后效果:

PS:整體功能還很簡陋,之后慢慢豐富吧T_T