Htmlagilitypack Official document (i) "Parser"

Source: Internet
Author: User
Tags pack

The HTML parser allows you to parse HTML and return a HTMLDocument. Html Parser

Name Description
From File Loads an HTML document from a file.
From String Loads the HTML document from the specified string.
From Web Gets an HTML document from Internet resource.
From Browser Gets an HTML document from a WebBrowser.
loading HTML from a file

Htmldocument.load method to load an HTML document from a file

The following example loads HTML from a file

var path = @ "test.html";

var doc = new HTMLDocument ();
Doc. Load (path);

var node = doc. Documentnode.selectsinglenode ("//body");

Console.WriteLine (node. outerHTML);  
loading HTML from a string

The Htmldocument.loadhtml method loads the HTML document from the specified string.

The following example loads HTML from a specified string.

var html = @ "<! DOCTYPE html>
load HTML from the Web

The Htmlweb.load method gets an HTML document from an Internet resource.

The following example loads HTML from the Web

var html = @ "http://html-agility-pack.net/";

Htmlweb Web = new Htmlweb ();

var htmldoc = web. Load (HTML);

var node = htmlDoc.DocumentNode.SelectSingleNode ("//head/title");

Console.WriteLine ("Node Name:" + node.) Name + "\ n" + node. outerHTML);
loading HTML from WebBrowser

The Htmlweb.load method obtains an HTML document from a Web browser. You can wait for JavaScript to run by customizing the isbrowserscriptcompleted parameter.

The following example loads HTML from WebBrowser and waits until the text is set to Div.

string url = "Http://html-agility-pack/from-browser";
var web1 = new Htmlweb (); var Doc1 = Web1.

    Loadfrombrowser (URL, o => {var webBrowser = (webBrowser) o; Wait until the dynamic text is set to return!string. IsNullOrEmpty (WebBrowser.Document.GetElementById ("Uidynamictext").
InnerText);
}); var T1 = Doc1. Documentnode.selectsinglenode ("//div[@id = ' uidynamictext ']").

InnerText;
var web2 = new Htmlweb (); var doc2 = web2. Loadfrombrowser (URL, HTML => {//wait until dynamic text is set to return!html.
Contains ("<div id=\" uidynamictext\ "></div>");
}); var t2 = doc2. Documentnode.selectsinglenode ("//div[@id = ' uidynamictext ']").

InnerText;
Console.WriteLine ("Text 1:" + T1); Console.WriteLine ("Text 2:" + T2); 

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.