經驗總結21--抓取WEB資料,匯率,HtmlAgilityPack

來源:互聯網
上載者:User

標籤:agent   nbsp   mod   tty   類比請求   sel   取數   name   windows   

網上找了非常多資料,PHP的比較多,然後找到有csv檔案的。處理起來非常麻煩,國外的網站速度非常慢。

最後還是去頁面上抓取資料,我是從中國銀行抓取的,各位可去其它網站抓取。

1、類比請求URL。

string url = "http://srh.bankofchina.com/search/whpj/search.jsp?

pjname=1316";

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                request.ContentType = "text/html; charset=UTF-8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string result = myStreamReader.ReadToEnd();

2、對HTML進行處理。

使用HtmlAgilityPack進行輔助。

HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(result);
                foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//td"))
                {
                    if (node.InnerText == "美元")
                    {
                        result = node.NextSibling.NextSibling.InnerText;
                        break;
                    }
                }

具體資料位元置。自己可先用F12尋找到。然後使用工具進行尋找,具體文檔自己去查,這種方法臨時擷取到USD-CNY的匯率。


經驗總結21--抓取WEB資料,匯率,HtmlAgilityPack

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.