【原创】ASP.NET C# 盗取中国银行汇率表
//注意:本文为博主原创,转载请注明出处,谢谢合作!
前台:
<script src="http://www.cnblogs.com/JavaScript/jquery/jquery.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/JavaScript/jquery/jquery.pager.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
GetAjaxTable(1);
});
function GetAjaxTable(pageclickednumber) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/WebService/WebService.asmx/GetHL",
data: "{pageindex:" + pageclickednumber + "}",
dataType: "json",
success: function(d) {
if ($(d).text() != null) {
$("#get_ajax_table tr").each(function() {
if ($(this).attr("class") != "header" && $(this).attr("class") != "pager")
$(this).remove();
});
$(".pager").before($(d).find("tbody").html());
$(".nav").removeAttr("width").removeAttr("class");
$("#pager").pager({
pagenumber: pageclickednumber,
pagecount: 20,
buttonClickCallback: function(currentpage) { $("#pageindex").text(currentpage); GetAjaxTable(currentpage); }
});
}
else {
alert("数据加载失败,请重新尝试!");
}
},
error: function() { alert("数据加载超时,请重试!"); }
});
}
</script>
<div style="height: 30px; font-weight: bold; line-height: 30px; text-align: left;
color: White; padding-left: 15px; background-color: #92b000;">
以下汇率清单为 <a href="http://www.boc.cn/sourcedb/whpj/index.html" target="_blank" style="float: none;">
中国银行</a> 当日银行汇率表。
</div>
<table bgcolor="#ffffff" border="1" bordercolordark="#ffffff" bordercolorlight="#cccccc"
cellpadding="1" cellspacing="0" width="100%" id="get_ajax_table" rules="all"
frame="box">
<tr class="header">
<td align="center" style="font-weight: bold">
货币名称
</td>
<td align="center" style="font-weight: bold">
现汇买入价
</td>
<td align="center" style="font-weight: bold">
现钞买入价
</td>
<td align="center" style="font-weight: bold">
卖出价
</td>
<td align="center" style="font-weight: bold">
基准价
</td>
<td align="center" style="font-weight: bold">
中行折算价
</td>
<td align="center" style="font-weight: bold">
发布日期
</td>
<td align="center" style="font-weight: bold">
发布时间
</td>
</tr>
<tr class="pager">
<td colspan="8">
<div id="pager_left">
当前第 <b id="pageindex">1</b>/<b id="pagecount">20</b> 页
</div>
<div id="pager">
</div>
</td>
</tr>
</table>
后台:
//获取中行汇率
[WebMethod]
public string GetHL(int pageindex)
{
string hl = null;
try
{
string tempurl = "http://www.boc.cn/sourcedb/whpj/index.html";
if (pageindex > 1)
tempurl = "http://www.boc.cn/sourcedb/whpj/index_" + (pageindex - 1).ToString() + ".html";
HttpWebRequest webr = (HttpWebRequest)WebRequest.Create(tempurl);//创建请求
HttpWebResponse wb = (HttpWebResponse)webr.GetResponse();
Stream sr = wb.GetResponseStream();//得到返回数据流
StreamReader sr1 = new StreamReader(sr, Encoding.GetEncoding("utf-8"));//用于读取数据流的内容
string zz = sr1.ReadToEnd();//读取完成
sr1.Close();
wb.Close();//关闭
string temp = "<table width=\"880\" id=\"documentContainer\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\" bgcolor=\"#EAEAEA\">[\\s\\S]*?</table>";
hl = Regex.Match(zz, temp, RegexOptions.IgnoreCase).Value;
return hl;
}
catch { return null; }
}
注意:
代码已测试通过,功能完美,若需帮助QQ:61149012