{"id":41601,"date":"2024-12-01T08:36:37","date_gmt":"2024-12-01T00:36:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41601\/"},"modified":"2024-12-01T08:36:37","modified_gmt":"2024-12-01T00:36:37","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%a0%87%e7%ad%be%e7%9a%84%e5%86%85%e9%83%a8-html-%e6%88%96%e6%96%87%e6%9c%ac%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41601\/","title":{"rendered":"\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 19:48:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4eec\u5982\u4f55\u6839\u636e\u4e0b\u9762\u7684\u793a\u4f8b\u83b7\u53d6\u951a\u6587\u672c\u7684\u503c\uff1f\u8fd9\u662f\u6211\u7684\u4ee3\u7801\u3002\u6211\u53ef\u4ee5\u4f7f\u7528 <code>html.elementnode<\/code> \u83b7\u53d6 <code>href<\/code> \u548c <code>title<\/code> \u7684\u503c\u3002\u6211\u9700\u8981\u4ec5\u4f7f\u7528 <code>golang.org\/x\/net\/html<\/code> \u6765\u83b7\u53d6\u6587\u672c\u7684\u503c\uff0c\u800c\u4e0d\u4f7f\u7528\u5176\u4ed6\u5e93\u3002<\/p>\n<p>\u793a\u4f8b\uff1a\u4ece <code>&lt;a href=\"https:xyz.com\"&gt;text xyz&lt;\/a&gt;<\/code> \u4e2d\uff0c\u6211\u60f3\u83b7\u53d6\u201ctext xyz\u201d\u3002<\/p>\n<pre>\/\/ html.ElementNode works for getting href and title value but no text value with TextNode. \nif n.Type == html.TextNode &amp;&amp; n.Data == \"a\" {\n    for _, a := range n.Attr {\n        if a.Key == \"href\" {\n            text = a.Val\n        }\n    }\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u7ed9\u5b9a html\uff1a<\/p>\n<pre>&lt;a href=\"http:\/\/example.com\/1\"&gt;go to &lt;b&gt;example&lt;\/b&gt; 1&lt;\/a&gt;\n&lt;p&gt;some para text&lt;\/p&gt;\n&lt;a href=\"http:\/\/example.com\/2\"&gt;go to &lt;b&gt;example&lt;\/b&gt; 2&lt;\/a&gt;\n<\/pre>\n<p>\u60a8\u53ea\u5e0c\u671b\u770b\u5230\u6587\u5b57\u5417\uff1f<\/p>\n<pre>go to example 1\ngo to example 2\n<\/pre>\n<p>\u60a8\u671f\u671b\u5185\u90e8 html \u5417\uff1f<\/p>\n<pre>go to &lt;b&gt;example&lt;\/b&gt;example 1\ngo to &lt;b&gt;example&lt;\/b&gt;example 2\n<\/pre>\n<p>\u6216\u8005\uff0c\u4f60\u8fd8\u671f\u5f85\u522b\u7684\u5417\uff1f<\/p>\n<p>\u4ee5\u4e0b\u7a0b\u5e8f\u4ec5\u63d0\u4f9b\u6587\u672c\u6216\u5185\u90e8 html\u3002\u6bcf\u6b21\u627e\u5230\u951a\u8282\u70b9\u65f6\uff0c\u5b83\u90fd\u4f1a\u4fdd\u5b58\u8be5\u8282\u70b9\uff0c\u7136\u540e\u7ee7\u7eed\u6cbf\u7740\u8be5\u8282\u70b9\u7684\u6811\u5411\u4e0b\u79fb\u52a8\u3002\u5f53\u5b83\u9047\u5230\u5176\u4ed6\u8282\u70b9\u65f6\uff0c\u5b83\u4f1a\u68c0\u67e5\u5df2\u4fdd\u5b58\u7684\u8282\u70b9\uff0c\u5e76\u9644\u52a0 textnodes \u7684\u6587\u672c\u6216\u5c06\u8282\u70b9\u7684 html \u5448\u73b0\u5230\u7f13\u51b2\u533a\u3002\u6700\u540e\uff0c\u5728\u904d\u5386\u6240\u6709\u5b50\u8282\u70b9\u5e76\u91cd\u65b0\u9047\u5230\u4fdd\u5b58\u7684\u951a\u8282\u70b9\u540e\uff0c\u5b83\u4f1a\u6253\u5370\u6587\u672c\u5b57\u7b26\u4e32\u548c html \u7f13\u51b2\u533a\uff0c\u91cd\u7f6e\u4e24\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u5c06\u951a\u8282\u70b9\u6e05\u96f6\u3002<\/p>\n<p>\u6211\u60f3\u5230\u4e86\u4f7f\u7528\u7f13\u51b2\u533a\u548c html.render\uff0c\u5e76\u4fdd\u5b58\u6765\u81ea  \u7684\u7279\u5b9a\u8282\u70b9\u3002<\/p>\n<p>\u4ee5\u4e0b\u5185\u5bb9\u4e5f\u5728  \u4e2d\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"bytes\"\n    \"fmt\"\n    \"io\"\n    \"strings\"\n\n    \"golang.org\/x\/net\/html\"\n)\n\nfunc main() {\n    s := `\n    &lt;a href=\"http:\/\/example.com\/1\"&gt;go to &lt;b&gt;example&lt;\/b&gt; 1&lt;\/a&gt;\n    &lt;p&gt;some para text&lt;\/p&gt;\n    &lt;a href=\"http:\/\/example.com\/2\"&gt;go to &lt;b&gt;example&lt;\/b&gt; 2&lt;\/a&gt;\n    `\n\n    doc, _ := html.parse(strings.newreader(s))\n\n    var nanchor *html.node\n    var stxt string\n    var bufinnerhtml bytes.buffer\n\n    w := io.writer(&amp;bufinnerhtml)\n\n    var f func(*html.node)\n    f = func(n *html.node) {\n        if n.type == html.elementnode &amp;&amp; n.data == \"a\" {\n            nanchor = n\n        }\n\n        if nanchor != nil {\n            if n != nanchor { \/\/ don't write the a tag and its attributes\n                html.render(w, n)\n            }\n            if n.type == html.textnode {\n                stxt += n.data\n            }\n        }\n\n        for c := n.firstchild; c != nil; c = c.nextsibling {\n            f(c)\n        }\n\n        if n == nanchor {\n            fmt.println(\"text:\", stxt)\n            fmt.println(\"innerhtml:\", bufinnerhtml.string())\n            stxt = \"\"\n            bufinnerhtml.reset()\n            nanchor = nil\n        }\n    }\n    f(doc)\n}\n<\/pre>\n<pre>Text: Go to example 1\nInnerHTML: Go to &lt;b&gt;example&lt;\/b&gt;example 1\nText: Go to example 2\nInnerHTML: Go to &lt;b&gt;example&lt;\/b&gt;example 2\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f \u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-20 19:48:34 0\u6d4f\u89c8 \u6536\u85cf \u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60Golang\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587\u300a\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u90e8 HTML \u6216\u6587\u672c\uff1f\u300b\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0Golang\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u4eec\u5982\u4f55\u6839\u636e\u4e0b\u9762\u7684\u793a\u4f8b\u83b7\u53d6\u951a\u6587\u672c\u7684\u503c\uff1f\u8fd9\u662f\u6211\u7684\u4ee3\u7801\u3002\u6211\u53ef\u4ee5\u4f7f\u7528 html.elementnode \u83b7\u53d6 href \u548c title \u7684\u503c\u3002\u6211\u9700\u8981\u4ec5\u4f7f\u7528 golang.org\/x\/net\/html \u6765\u83b7\u53d6\u6587\u672c\u7684\u503c\uff0c\u800c\u4e0d\u4f7f\u7528\u5176\u4ed6\u5e93\u3002 \u793a\u4f8b\uff1a\u4ece &lt;a href=&#8221;https:xyz.com&#8221;&gt;text xyz&lt;\/a&gt; \u4e2d\uff0c\u6211\u60f3\u83b7\u53d6\u201ctext xyz\u201d\u3002 \/\/ html.ElementNode works for getting href and title value but no text value with TextNode. if n.Type == html.TextNode &amp;&amp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41601","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41601"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41601\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}