HtmlAgilityPack: HTML operation class library

Source: Internet
Author: User

HtmlAgilityPack: HTML operation class library

1. Read the html webpage content on the network, obtain the html in the element body of the webpage, process the src attribute of all img elements, and return it as a string

If (l_sWenBenHtmlFtpPath.Substring (l_sWenBenHtmlFtpPath.LastIndexOf (". ") + 1) =" html ") {HtmlWeb htmlWeb = new HtmlWeb (); HtmlDocument htmlDoc = htmlWeb. load (l_sWenBenHtmlFtpPath); HtmlNode htmlNode = htmlDoc. documentNode; HtmlNodeCollection nodes = htmlNode. selectNodes ("// body"); // use the xpath syntax to query if (nodes! = Null) {foreach (HtmlNode bodyTag in nodes) {HtmlNodeCollection nodes2 = htmlNode. SelectNodes ("// img"); // query if (nodes2! = Null) {foreach (HtmlNode imgTag in nodes2) {string imgHttpPath = imgTag. attributes ["src"]. value; imgTag. attributes ["src"]. value = l_sWenBenHtmlFtpPath.Substring (0, l_sWenBenHtmlFtpPath.LastIndexOf ("/") + 1) + imgHttpPath;} l_sWenBenHtml = bodyTag. innerHtml ;}}}

2. Use the HtmlAgilityPack Html operation class library to load html strings as html document objects, and then perform operations on html dom.

// 1. decodes the html string sDecodeString = HttpUtility. htmlDecode (HttpUtility. urlDecode (sEncodeString); // 2. concatenate the complete html string sDecodeString = @ "<! DOCTYPE html> + SDecodeString + @ "</div> </body> // 5. Save the string to an html file
// Do something

 

Continuously improving and updating. Please wait...

Related Article

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.