使用HtmlAgilityPack批量抓取網頁資料

來源:互聯網
上載者:User

標籤:ext   request   ring   validate   軟體   取數   lin   迴圈   back   

【轉】使用HtmlAgilityPack批量抓取網頁資料

相關軟體點擊下載

登入的處理。因為有些網頁資料需要登陸後才能提取。這裡要使用ieHTTPHeaders來提取登入時的提交資訊。

抓取網頁

 HtmlAgilityPack.HtmlDocument htmlDoc;

            if (!string.IsNullOrEmpty(登入URL))
            {
                htmlDoc = htmlWeb.Load(登入URL, 提交的使用者驗證資訊, 擷取資料的網頁URL);
            }
            else
            {
                htmlDoc = htmlWeb.Load(擷取資料的網頁URL);
            }
        

 

 

 ArrayList list = new ArrayList();
            list.add("//table/tr[1]/td");
            list.add("//table/tr[2]/td");
            //擷取迴圈的節點的xpath,比如://table/tr
            HtmlNodeCollection repeatNodes = htmlDoc.DocumentNode.SelectNodes("//table/tr");

            //迴圈節點
            foreach (HtmlNode node in repeatNodes)
            {
                //迴圈擷取資料
                foreach (string dataPath in list)
                {

                    HtmlNode dataNode = node.SelectSingleNode(list);
                    if (dataNode != null)
                    {
                        string text = dataNode.InnerText;
                    }

                }
            }

如果出現亂碼,調整編碼集為gb2312或者是utf-8

htmlWeb.DefaultEncoding = System.Text.Encoding.GetEncoding(strEncode);


-------------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TestTools.WebTesting;

using HtmlAgilityPack;

publicclassWebTest1Coded : WebTest

{

publicoverrideIEnumerator<WebTestRequest> GetRequestEnumerator()

{

WebTestRequest request1 = newWebTestRequest("http://www.microsoft.com/");

request1.ValidateResponse += newEventHandler<ValidationEventArgs>(request1_ValidateResponse);

yieldreturn request1;

}

void request1_ValidateResponse(object sender, ValidationEventArgs e)

{

//load the response body string as an HtmlAgilityPack.HtmlDocument

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

doc.LoadHtml(e.Response.BodyString);

//locate the "Nav" element

HtmlNode navNode = doc.GetElementbyId("Nav");

//pick the first <li> element

HtmlNode firstNavItemNode = navNode.SelectSingleNode(".//li");

//validate the first list item in the Nav element says "Windows"

e.IsValid = firstNavItemNode.InnerText == "Windows";

}

}

使用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.