Open source project HTML Agility Pack for fast parsing of HTML

Source: Internet
Author: User

This is a very good thing, used to do HTML parsing is in use Htmlparser, although handy, but the resolution speed is slow, happened to find this today, take over to try, all unexpectedly, very cool, recommended for you to use.

Here are some simple use tips, hope to be useful to everyone, I personally also a learning process.

Why Html Agility Pack? (hereinafter referred to as HAP)

. NET parsing of HTML files has many options, including Microsoft itself also provides mshtml for manipulate HTML files. However, after a period of searching, the HTML Agility pack surfaced: It is the most recommended C # Html parser on the StackOverflow website. Hap Open source, easy to use, resolution fast.

How do I use HAP?

1. Download http://htmlagilitypack.codeplex.com/

2. Unzip

3. In Visual Studio solution, right-click Add Reference, Project, select HTMLAgilityPack.dll in the Unzip folder, OK

4. The code head joins using Htmlagilitypack;

done!

  1. Htmlweb webClient = new Htmlweb ();
  2. HTMLDocument doc = webclient.load ("http://xxx");
  3. Htmlnodecollection hreflist = doc.    Documentnode.selectnodes (".//a[@href]");
  4. if (hreflist! = null)
  5. {
  6. foreach (Htmlnode href in hreflist)
  7. {
  8. Htmlattribute att = href.    attributes["href"];
  9. DoSomething (Att. Value);
  10. }
  11. }

Q: How do I select HTML nodes by ID?

A: Using @id= ' xxx ', e.g.,

    1. Htmlnode bugsum = doc.    Documentnode.selectsinglenode ("//h2[@id = ' summary ']");

Q: How do I get the text content or HTML content of a node?

    1. Node. Innertext.trim ()
    2. Node. InnerHtml
    3. Node. outerHTML

Q: How do I find nodes under the HTML tree structure?

A: For example, find the first table under Id=container div from the root node:

    1. Htmlnode table = doc.    Documentnode.selectsinglenode ("//div[@id = ' container ']/table[1]");

Note that the "//" in the path means finding from the root node, two slashes '//' means finding all childnodes, and a slash '/' means finding only the first layer of childnodes (that is, not looking for grandchild); dot slash "./" Represents the start of a lookup from the current node rather than the root node. Next line of code, such as the TR to find all the direct child nodes of the table:

    1. htmlnodecollection tr = table.   SelectNodes ("./tr");

Q: How do I get the ID of a node?

A: Very simple: node.id

Q: If a piece of HTML exists in a string, is it possible to use HTML Agility pack for processing?

A: Yes, first load the string in, then the same way:

    1. <pre name="code" class="CSharp" >//load the original HTML
    2. String html = "Some HTML stuff"
    3. HTMLDocument doc = new HTMLDocument ();
    4. Doc. Loadhtml (@html);

Q: I've done some processing of the HTML load coming in, such as changing some of the node content, deleting some of the nodes, and why the results haven't changed?

A: Maybe you forgot to save your changes to HTML, assuming that the HTML exists in the string:

  1. Load the original HTML
  2. String html = "Some HTML stuff"
  3. HTMLDocument doc = new HTMLDocument ();
  4. Doc. Loadhtml (@html);
  5. Make some changes
  6. DoSomething ();
  7. Save the Change
  8. var sb = new StringBuilder ();
  9. using (var writer = new StringWriter (SB))
  10. {
  11. Doc. Save (writer);
  12. }

Q: How do I get rid of the outer HTML tag leaving only content?

A: Use the Remove method. Suppose the node <a href=xxx>abcd</a> you want to leave the ABCD instead of <a></a> then you need to get this HTML node first, assuming that it's called Link:

    1. Link.    Parentnode.removechild (link,true);

The parameter true indicates leaving grandchild, where the content is ABCD; False means that the node is deleted along with its grandchilds.

There are many rules, the Internet provides the source code, you can study, and the source code has garbled problem, is the character set problem, only need to write a method to automatically judge can solve the

Open source project HTML Agility Pack for fast parsing of HTML

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.