【原创】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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值