Use htmlagilitypack to capture web page data in batches

Source: Internet
Author: User
Click to download related software

Login processing. Because some webpage data can be extracted only after login. Here, you need to use iehttpheaders to extract the submission information for logon.

Capture webpages

Htmlagilitypack. htmldocument htmldoc;

If ( ! String . Isnullorempty (logon URL ))
{
Htmldoc=Htmlweb. Load (logon URL, submitted user authentication information, and webpage URL for data retrieval );
}
Else
{
Htmldoc=Htmlweb. Load (URL of the webpage that obtains data );
}

 

Arraylist list =   New Arraylist ();
List. Add ( " // Table/TR [1]/TD " );
List. Add ( " // Table/TR [2]/TD " );
// Obtain the XPath of the cyclic node, for example: // Table/tr
Htmlnodecollection repeatnodes = Htmldoc. documentnode. selectnodes ( " // Table/tr " );

// Cyclic nodes
Foreach (Htmlnode Node In Repeatnodes)
{
// Obtain data cyclically
Foreach ( String Datapath In List)
{

Htmlnode datanode = Node. selectsinglenode (list );
If (Datanode ! =   Null )
{
StringText=Datanode. innertext;
}

}
}

If garbled characters appear, adjust the encoding set to gb2312 or UTF-8.

Htmlweb. defaultencoding = System. Text. encoding. getencoding (strencode );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.