﻿{"id":954,"date":"2014-06-06T10:59:51","date_gmt":"2014-06-06T02:59:51","guid":{"rendered":"http:\/\/notes.zerobox.org\/?p=954"},"modified":"2014-06-06T10:59:51","modified_gmt":"2014-06-06T02:59:51","slug":"html%e8%a7%a3%e6%9e%90%e5%88%a9%e5%99%a8-htmlagility%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/954.html","title":{"rendered":"HTML\u89e3\u6790\u5229\u5668-HtmlAgility\u5b66\u4e60"},"content":{"rendered":"<p style=\"color: #000000;\">HtmlAgility\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Html\u89e3\u6790\u5e93\uff0c\u636e\u8bf4\u662fC#\u7248\u7684JQuery\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n<p style=\"color: #000000;\">\u8be5\u7bc7\u5b66\u4e60\u5b83\u7684\u89e3\u6790\u529f\u80fd\uff0c\u8fd8\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u8bf7\u6c42,\u521b\u5efahtml,\u8bbe\u7f6e\u4ee3\u7406\u7b49\u7b49\uff0c\u6682\u5148\u4e0d\u7814\u7a76\u3002<\/p>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"color: #000000;\"><strong>1.\u7b80\u5355\u4f8b\u5b50<\/strong><\/p>\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a style=\"color: #075db3;\" title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/notes.zerobox.org\/wp-content\/uploads\/2014\/06\/9148256b89e79e5d03bf03a93cf12ac42.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Net;\r\n\r\nusing HtmlAgilityPack;\r\n\r\nnamespace ConsoleApplication1\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            HtmlWeb webClient = new HtmlWeb();\r\n            HtmlDocument doc = webClient.Load(\"http:\/\/www.baidu.com\");\r\n            var rootNode = doc.DocumentNode;\r\n            HtmlNodeCollection categoryNodeList = rootNode.SelectNodes(\"\/\/html[1]\/body[1]\");\r\n            foreach (var item in categoryNodeList)\r\n            {\r\n                Console.WriteLine(\"item: \" + item.Name);\r\n            }\r\n\r\n            Console.Read();\r\n        }\r\n    }\r\n}<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a style=\"color: #075db3;\" title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/notes.zerobox.org\/wp-content\/uploads\/2014\/06\/9148256b89e79e5d03bf03a93cf12ac42.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p style=\"color: #000000;\">\u7b97\u662f\u7b2c\u4e00\u4e2aHellow world\uff0c\u6252\u7684\u767e\u5ea6\u9875\u9762\u3002<\/p>\n<p style=\"color: #000000;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"color: #000000;\"><strong>2.\u8bfb\u53d6<\/strong><\/p>\n<p style=\"color: #000000;\">\u90a3\u4e48\uff0c\u5982\u679c\u662f\u8f7d\u5165\u672c\u5730\u7684Html\u6216\u8005\u76f4\u63a5\u8bfb\u6d41\uff0c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u8fd9\u4e48\u505a<\/p>\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<pre>HtmlDocument doc = new HtmlDocument();\r\ndoc.Load(@\"D:\\xxx.mht\", Encoding.UTF8, false);<\/pre>\n<\/div>\n<p style=\"color: #000000;\">\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<pre>public void LoadHtml(string html);\/\/\u76f4\u63a5\u8bfb\u5b57\u7b26\u4e32\u5316\u7684html\r\npublic void Load(Stream stream);\/\/\u6d41\r\npublic void Load(string path);\/\/\u672c\u5730\u8def\u5f84<\/pre>\n<\/div>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">HtmlDocumen\u5176\u672c\u8eab\u4e5f\u63d0\u4f9b\u68c0\u6d4b\u7f16\u7801\u7684\u65b9\u6cd5\u3002<\/p>\n<p style=\"color: #000000;\">HtmlWeb\u4e3b\u8981\u662f\u81ea\u52a8\u68c0\u6d4b\u7f16\u7801\uff0c\u5982\u679c\u8981\u81ea\u5b9a\u4e49\u7f16\u7801\u53ef\u4ee5\u6539\u5176\u4e2d\u5c5e\u6027\u3002OverrideEncoding,\u00a0AutoDetectEncoding\u3002\u800cHtmlDocument\u5bf9\u7f16\u7801\u7684\u64cd\u4f5c\u53cd\u800c\u4e0d\u4e00\u6837\uff0c\u6307\u5b9a\u5728\u53c2\u6570\u4e2d\uff0c\u4f30\u8ba1\u662f\u81ea\u52a8\u68c0\u6d4b\u7f16\u7801\u5df2\u7ecf\u5f88\u5f3a\u5927\u4e86\u628a\uff0c\u5f88\u5c11\u8981\u81ea\u5df1\u6307\u5b9a\u3002\u3002\u3002\u3002<\/p>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"color: #000000;\"><strong>3.\u8282\u70b9\u9009\u62e9<\/strong><\/p>\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<pre>rootNode.SelectNodes\r\nrootNode.SelectSingleNode<\/pre>\n<\/div>\n<p style=\"color: #000000;\">\u9009\u62e9\u8282\u70b9\u548c\u9009\u62e9\u5355\u4e2a\u8282\u70b9\u3002<\/p>\n<p style=\"color: #000000;\">\u7528SelectNodes\u4e3a\u4f8b\uff0c\u770b\u4e00\u4e0b\u53c2\u6570<\/p>\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<pre>rootNode.SelectNodes(\"\/\/html[1]\/body[1]\");<\/pre>\n<\/div>\n<p style=\"color: #000000;\">&#8220;\/\/&#8221;\u53cc\u659c\u6760\u8868\u793a\u4ece\u6839\u8282\u70b9\u67e5\u627e\u6240\u6709\u5b50\u8282\u70b9<\/p>\n<p style=\"color: #000000;\">&#8220;\/&#8221;\u5355\u659c\u6760\u8868\u793a\u53ea\u67e5\u627e\u7b2c\u4e00\u5c42\u5b50\u8282\u70b9<\/p>\n<p style=\"color: #000000;\">&#8220;.\/&#8221;\u70b9\u659c\u6760\u8868\u793a\u4ece\u5f53\u524d\u8282\u70b9\u5f00\u59cb\u67e5\u627e<\/p>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">[]\u4e2d\u62ec\u53f7\u4e2d\u7684\u4ee3\u8868\u76f8\u540c\u540d\u5b57\u7684\u5b50\u8282\u70b9\u7d22\u5f15\u3002<\/p>\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<pre>var resultList = rootNode.SelectNodes(\"\/\/html[1]\/body[1]\/div[1]\/div[position()&lt;5]\");\/\/\u53d6\u524d4\u4e2a\u5143\u7d20\r\nresultList = rootNode.SelectNodes(\"\/\/html[1]\/body[1]\/div[1]\/div[last()]\");\/\/\u53d6\u6700\u540e1\u4e2a\u5143\u7d20\r\nresultList = rootNode.SelectNodes(\"\/\/html[1]\/body[1]\/div[1]\/div[@id]\");\/\/\u53d6\u6240\u6709\u6709id\u5c5e\u6027\u7684\u5143\u7d20\r\nresultList = rootNode.SelectNodes(\"\/\/html[1]\/body[1]\/div[1]\/div[@id='head']\");\/\/\u53d6\u5c5e\u6027id\u503c\u4e3ahead\u7684\u5143\u7d20<\/pre>\n<\/div>\n<p style=\"color: #000000;\">\u66f4\u591a\u5c5e\u6027\u53ef\u4ee5\u5728W3SCHOOL\u67e5\u770b<a style=\"color: #075db3;\" href=\"http:\/\/www.w3school.com.cn\/xpath\/xpath_functions.asp\">http:\/\/www.w3school.com.cn\/xpath\/xpath_functions.asp<\/a><\/p>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">\u00a0\u53d6\u5c5e\u6027<\/p>\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<pre>doc.Attributes[\"id\"];<\/pre>\n<\/div>\n<p style=\"color: #000000;\">\u00a0\u53d6\u5143\u7d20<\/p>\n<div class=\"cnblogs_code\" style=\"color: #000000;\">\n<pre>doc.GetElementbyId(\"id\");<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>HtmlAgility\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Html\u89e3\u6790\u5e93\uff0c\u636e\u8bf4\u662fC#\u7248\u7684JQuery\uff0c\u529f &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/954.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[228],"class_list":["post-954","post","type-post","status-publish","format-standard","hentry","tag-html"],"views":473,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=954"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/954\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=954"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}