{"id":322,"date":"2016-03-31T14:16:32","date_gmt":"2016-03-31T06:16:32","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=322"},"modified":"2016-03-31T17:14:14","modified_gmt":"2016-03-31T09:14:14","slug":"%e7%88%ac%e8%99%ab%e8%a7%a3%e6%9e%90%e7%bd%91%e9%a1%b5%e5%86%85htmlparser%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=322","title":{"rendered":"\u722c\u866b\u89e3\u6790\u7f51\u9875\u5185\u5bb9HTMLParser\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"<p>\n\tHTML\u672c\u8d28\u4e0a\u662fXML\u7684\u5b50\u96c6\uff0c\u4f46\u662fHTML\u7684\u8bed\u6cd5\u6ca1\u6709XML\u90a3\u4e48\u4e25\u683c\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528\u6807\u51c6\u7684DOM\u6216SAX\u6765\u89e3\u6790HTML\u3002\u8fd9\u65f6HTMLParser\u8bde\u751f\u4e86\u3002<br \/>\n\tHTMLParser\u662f\u4e00\u4e2a\u7528\u6765\u89e3\u6790HTML\u6587\u6863\u7684\u5f00\u653e\u6e90\u7801\u9879\u76ee\uff0c\u5b83\u5177\u6709\u5c0f\u5de7\u3001\u5feb\u901f\u3001\u4f7f\u7528\u7b80\u5355\u7684\u7279\u70b9\u4ee5\u53ca\u62e5\u6709\u5f3a\u5927\u7684\u529f\u80fd\u3002\u7f3a\u70b9\u662f\u76f8\u5173\u6587\u6863\u6bd4\u8f83\u5c11\uff08\u82f1\u6587\u7684\u4e5f\u5c11\uff09\uff0c\u5f88\u591a\u529f\u80fd\u9700\u8981\u81ea\u5df1\u6478\u7d22\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\u8fd8\u662f\u8981\u8d39\u4e00\u4e9b\u529f\u592b\u7684\uff0c\u800c\u4e00\u65e6\u4e0a\u624b\u4ee5\u540e\uff0c\u4f1a\u53d1\u73b0HTMLParser\u7684\u7ed3\u6784\u8bbe\u8ba1\u5f88\u5de7\u5999\uff0c\u975e\u5e38\u5b9e\u7528\uff0c\u57fa\u672c\u4f60\u7684\u5404\u79cd\u9700\u6c42\u90fd\u53ef\u4ee5\u6ee1\u8db3\u3002\u53e6\u5916HTMLParser\u76ee\u524d\u5df2\u57fa\u672c\u4e0d\u66f4\u65b0\u4e86\uff0c\u5b83\u5df2\u88abjsoup(\u5173\u4e8ejsoup\u8bf7\u9605\u8bfb\uff1a<a href=\"http:\/\/www.xttblog.com\/?p=326\" target=\"_blank\">http:\/\/www.xttblog.com\/?p=326<\/a>)\u53d6\u4ee3\u4e86\u3002<br \/>\n\t\u4e00\u4e2aHTML\u6587\u6863\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u6807\u7b7e\u5dee\u4e0d\u591a\u5728HTMLParser\u4e2d\u90fd\u6709\u5bf9\u5e94\u7684\u7c7b\uff0c\u751a\u81f3\u5305\u62ec\u4e00\u4e9b\u52a8\u6001\u7684\u811a\u672c\u6807\u7b7e\uff0c\u4f8b\u5982 &lt;%%&gt; \u8fd9\u79cdJSP\u548cASP\u7528\u5230\u7684\u6807\u7b7e\u90fd\u6709\u76f8\u5e94\u7684JspTag\u5bf9\u5e94\u3002HTMLParser\u7684\u5f3a\u5927\u529f\u80fd\u8fd8\u4f53\u73b0\u5728\u4f60\u53ef\u4ee5\u4fee\u6539\u6bcf\u4e2a\u6807\u7b7e\u7684\u5c5e\u6027\u6216\u8005\u5b83\u6240\u5305\u542b\u7684\u6587\u672c\u5185\u5bb9\u5e76\u751f\u6210\u65b0\u7684 HTML \u6587\u6863\uff0c\u6bd4\u5982\u4f60\u53ef\u4ee5\u6587\u6863\u4e2d\u7684\u94fe\u63a5\u5730\u5740\u5077\u5077\u7684\u6539\u6210\u4f60\u81ea\u5df1\u7684\u5730\u5740\u7b49\u7b49\u3002<br \/>\n\tHTMLParser\u7684\u6838\u5fc3\u6a21\u5757\u662forg.htmlparser.Parser\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u5b9e\u9645\u5b8c\u6210\u4e86\u5bf9\u4e8eHTML\u9875\u9762\u7684\u5206\u6790\u5de5\u4f5c\u3002\u8fd9\u4e2a\u7c7b\u6709\u4e0b\u9762\u51e0\u4e2a\u6784\u9020\u51fd\u6570\uff1a<br \/>\n\t<span style=\"color:#FF0000\">public Parser ();<br \/>\n\tpublic Parser (Lexer lexer, ParserFeedback fb);<br \/>\n\tpublic Parser (URLConnection connection, ParserFeedback fb) throws ParserException;<br \/>\n\tpublic Parser (String resource, ParserFeedback feedback) throws ParserException;<br \/>\n\tpublic Parser (String resource) throws ParserException;<br \/>\n\tpublic Parser (Lexer lexer);<br \/>\n\tpublic Parser (URLConnection connection) throws ParserException;<br \/>\n\tpublic static Parser createParser (String html, String charset);<\/span>\n<\/p>\n<p>\n\t\u4f7f\u7528HtmlPaser\u7684\u5173\u952e\u6b65\u9aa4:<br \/>\n\t<span style=\"color:#000000\"><span style=\"font-size:14px\"><strong>a.\u901a\u8fc7Parser\u7c7b\u521b\u5efa\u4e00\u4e2a\u89e3\u91ca\u5668<br \/>\n\tb.\u521b\u5efaFilter\u6216\u8005Visitor<br \/>\n\tc.\u4f7f\u7528parser\u6839\u636efilter\u6216\u8005visitor\u6765\u53d6\u5f97\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9<br \/>\n\td.\u5bf9\u8282\u70b9\u5185\u5bb9\u8fdb\u884c\u5904\u7406<\/strong><\/span><\/span>\n<\/p>\n<p>\n\tHTMLParser\u5c06\u89e3\u6790\u8fc7\u7684\u4fe1\u606f\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u6811\u7684\u7ed3\u6784\u3002Node\u662f\u4fe1\u606f\u4fdd\u5b58\u7684\u6570\u636e\u7c7b\u578b\u57fa\u7840\u3002<br \/>\n\t<img decoding=\"async\" src=\"http:\/\/www.xttblog.com\/wp-content\/uploads\/2016\/03\/node.jpg\" \/>\n<\/p>\n<p>\n\tNode\u4e2d\u5305\u542b\u7684\u65b9\u6cd5\u6709\u51e0\u7c7b\uff1a<br \/>\n\t\u5bf9\u4e8e\u6811\u578b\u7ed3\u6784\u8fdb\u884c\u904d\u5386\u7684\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u6700\u5bb9\u6613\u7406\u89e3\uff1a<br \/>\n\tNode getParent ()\uff1a\u53d6\u5f97\u7236\u8282\u70b9<br \/>\n\tNodeList getChildren ()\uff1a\u53d6\u5f97\u5b50\u8282\u70b9\u7684\u5217\u8868<br \/>\n\tNode getFirstChild ()\uff1a\u53d6\u5f97\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9<br \/>\n\tNode getLastChild ()\uff1a\u53d6\u5f97\u6700\u540e\u4e00\u4e2a\u5b50\u8282\u70b9<br \/>\n\tNode getPreviousSibling ()\uff1a\u53d6\u5f97\u524d\u4e00\u4e2a\u5144\u5f1f\uff08\u4e0d\u597d\u610f\u601d\uff0c\u82f1\u6587\u662f\u5144\u5f1f\u59d0\u59b9\uff0c\u76f4\u8bd1\u592a\u9ebb\u70e6\u800c\u4e14\u4e0d\u7b26\u5408\u4e60\u60ef\uff0c\u5bf9\u4e0d\u8d77\u5973\u540c\u80de\u4e86\uff09<br \/>\n\tNode getNextSibling ()\uff1a\u53d6\u5f97\u4e0b\u4e00\u4e2a\u5144\u5f1f\u8282\u70b9<br \/>\n\t\u53d6\u5f97Node\u5185\u5bb9\u7684\u51fd\u6570\uff1a<br \/>\n\tString getText ()\uff1a\u53d6\u5f97\u6587\u672c<br \/>\n\tString toPlainTextString()\uff1a\u53d6\u5f97\u7eaf\u6587\u672c\u4fe1\u606f\u3002<br \/>\n\tString toHtml () \uff1a\u53d6\u5f97HTML\u4fe1\u606f\uff08\u539f\u59cbHTML\uff09<br \/>\n\tString toHtml (boolean verbatim)\uff1a\u53d6\u5f97HTML\u4fe1\u606f\uff08\u539f\u59cbHTML\uff09<br \/>\n\tString toString ()\uff1a\u53d6\u5f97\u5b57\u7b26\u4e32\u4fe1\u606f\uff08\u539f\u59cbHTML\uff09<br \/>\n\tPage getPage ()\uff1a\u53d6\u5f97\u8fd9\u4e2aNode\u5bf9\u5e94\u7684Page\u5bf9\u8c61<br \/>\n\tint getStartPosition ()\uff1a\u53d6\u5f97\u8fd9\u4e2aNode\u5728HTML\u9875\u9762\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e<br \/>\n\tint getEndPosition ()\uff1a\u53d6\u5f97\u8fd9\u4e2aNode\u5728HTML\u9875\u9762\u4e2d\u7684\u7ed3\u675f\u4f4d\u7f6e<br \/>\n\t\u7528\u4e8eFilter\u8fc7\u6ee4\u7684\u51fd\u6570\uff1a<br \/>\n\tvoid collectInto (NodeList list, NodeFilter filter)\uff1a\u57fa\u4e8efilter\u7684\u6761\u4ef6\u5bf9\u4e8e\u8fd9\u4e2a\u8282\u70b9\u8fdb\u884c\u8fc7\u6ee4\uff0c\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9\u653e\u5230list\u4e2d\u3002<br \/>\n\t\u7528\u4e8eVisitor\u904d\u5386\u7684\u51fd\u6570\uff1a<br \/>\n\tvoid accept (NodeVisitor visitor)\uff1a\u5bf9\u8fd9\u4e2aNode\u5e94\u7528visitor<br \/>\n\t\u7528\u4e8e\u4fee\u6539\u5185\u5bb9\u7684\u51fd\u6570\uff0c\u8fd9\u7c7b\u7528\u5f97\u6bd4\u8f83\u5c11\uff1a<br \/>\n\tvoid setPage (Page page)\uff1a\u8bbe\u7f6e\u8fd9\u4e2aNode\u5bf9\u5e94\u7684Page\u5bf9\u8c61<br \/>\n\tvoid setText (String text)\uff1a\u8bbe\u7f6e\u6587\u672c<br \/>\n\tvoid setChildren (NodeList children)\uff1a\u8bbe\u7f6e\u5b50\u8282\u70b9\u5217\u8868<br \/>\n\t\u5176\u4ed6\u51fd\u6570\uff1a<br \/>\n\tvoid doSemanticAction ()\uff1a\u6267\u884c\u8fd9\u4e2aNode\u5bf9\u5e94\u7684\u64cd\u4f5c\uff08\u53ea\u6709\u5c11\u6570Tag\u6709\u5bf9\u5e94\u7684\u64cd\u4f5c\uff09<br \/>\n\tObject clone ()\uff1a\u63a5\u53e3Clone\u7684\u62bd\u8c61\u51fd\u6570\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u662f\u4e00\u4e2a\u89e3\u6790\u6307\u5b9a\u8282\u70b9\u7684\u4f8b\u5b50\uff0chtml\u5185\u5bb9\uff1a\n<\/p>\n<pre>\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\t&lt;meta charset=&quot;UTF-8&quot;&gt;\r\n\t&lt;title&gt;HTMLParser\u89e3\u6790&mdash;&mdash;\u4e1a\u4f59\u8349&lt;\/title&gt;\r\n\t&lt;style type=&quot;text\/css&quot;&gt;\r\n\t\tbody{ \r\n\t\t\tbackground: #f5faff;\r\n\t\t}\r\n\t&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\t&lt;div class=&quot;xttblog&quot;&gt;\r\n\t\t&lt;div class=&quot;yeyucao&quot;&gt;&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n    \talert(&#039;ss&#039;);\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>\n\tjava\u89e3\u6790\u4ee3\u7801\uff1a\n<\/p>\n<pre>\r\npackage test;\r\n\r\nimport org.htmlparser.Node;\r\nimport org.htmlparser.NodeFilter;\r\nimport org.htmlparser.Parser;\r\nimport org.htmlparser.Tag;\r\nimport org.htmlparser.util.NodeList;\r\n\r\n\/**\r\n * HTMLParser\u89e3\u6790html\r\n * @author Herman.Xiong\r\n * @date 2016\u5e743\u670831\u65e513:49:27\r\n *\/\r\npublic class HTMLParserTest {\r\n\tpublic static void main(String[] args) {  \r\n\t\t\/\/\u89e3\u6790\u6307\u5b9a\u8282\u70b9\u7684\u4f8b\u5b50\r\n        try{  \r\n            Parser parser = new Parser(&quot;http:\/\/127.0.0.1:8080\/h5site\/6.html&quot;);\r\n            parser.setEncoding(&quot;GBK&quot;);\r\n            NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {\r\n\t\t\t\tprivate static final long serialVersionUID = 1L;\r\n\t\t\t\tpublic boolean accept(Node node) {\r\n                    return ((node instanceof Tag)\r\n                            &amp;&amp; !((Tag)node).isEndTag()\r\n                            &amp;&amp; ((Tag)node).getTagName().equals(&quot;DIV&quot;)\r\n                            &amp;&amp; ((Tag)node).getAttribute(&quot;class&quot;) != null\r\n                            &amp;&amp; ((Tag)node).getAttribute(&quot;class&quot;).equals(&quot;xttblog&quot;));\r\n                }\r\n            });\r\n            Node node = nodeList.elementAt(0);\r\n            System.out.println(node.toHtml());\r\n        } catch( Exception e ) {       \r\n            e.printStackTrace();\r\n        }  \r\n    }  \r\n}<\/pre>\n<p>\n\t\u539f\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.xttblog.com\/?p=322\" target=\"_blank\">http:\/\/www.xttblog.com\/?p=322<\/a><\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=322\">\u722c\u866b\u89e3\u6790\u7f51\u9875\u5185\u5bb9HTMLParser\u4f7f\u7528\u8be6\u89e3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>HTML\u672c\u8d28\u4e0a\u662fXML\u7684\u5b50\u96c6\uff0c\u4f46\u662fHTML\u7684\u8bed\u6cd5\u6ca1\u6709XML\u90a3\u4e48\u4e25\u683c\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528\u6807\u51c6\u7684DOM\u6216SAX\u6765\u89e3\u6790HTM [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":323,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[107,108,109],"class_list":["post-322","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-htmlparser","tag-108","tag-109"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=322"}],"version-history":[{"count":3,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/322\/revisions"}],"predecessor-version":[{"id":331,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/322\/revisions\/331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/media\/323"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}