繁体   English   中英

如何通过使用CSS或XPath选择器拒绝指定的HTML标签

[英]How to reject specify HTML tags by using css or xpath selector

我想通过使用css或xpath选择器来删除stylescript标签及其内容。

这是一个示例HTML:

<html>
  <head>
    <title>test</title>
    <style>
      // style
    </style>
    <script>
      /* some script */
    </script>
  </head>
  <body>
    <p>text</p>
    <script>
      /* some script */
    </script>
    <div>foo</div>
  </body>
</html>

我想要一个这样的HTML:

<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <p>text</p>
    <div>foo</div>
  </body>
</html>

我以为我可以用此代码获得不包含<script>标记的HTML,但是以某种方式该代码仅复制HTML。

doc = Nokogiri::HTML(open("foo.text"))
doc.css(":not(script)").to_html

如何启用我想要的行为?

尝试以下行:

doc.search('.//style').remove
doc.search('.//script').remove

更简单的是:

doc.search('style,script').remove

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM