删除inner_html中的注释
我有一些使用 Nokogiri 的代码,我试图在不获取注释的情况下获取 inner_html
。
html = Nokogiri::HTML(open(@sql_scripts_url[1])) #using first value of the array
html.css('td[class="ms-formbody"]').each do |node|
puts node.inner_html # prints comments
end
I have some code that uses Nokogiri and I am trying to get the inner_html
without getting the comments.
html = Nokogiri::HTML(open(@sql_scripts_url[1])) #using first value of the array
html.css('td[class="ms-formbody"]').each do |node|
puts node.inner_html # prints comments
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您没有提供任何示例 HTML 或所需的输出,因此这里有一个通用解决方案:
您可以使用
comment()
节点测试;您可以通过调用.remove
在所有评论节点上。图解:请注意,上面破坏性地修改了文档以删除注释。如果您希望保持原始文档不被修改,您也可以这样做:
最后,请注意,如果您不需要标记,则仅要求
text
本身不包含 HTML 注释:Since you have not provided any sample HTML or desired output, here's a general solution:
You can select SGML comments in XPath by using the
comment()
node test; you can strip them out of the document by calling.remove
on all comment nodes. Illustrated:Note that the above modifies the document destructively to remove the comments. If you wish to keep the original document unmodified, you could alternatively do this:
Finally, note that if you don't need the markup, just asking for the
text
itself does not include HTML comments: