开发者

How do I omit script elements from HTML using XPath in Nokogiri in Ruby on Rails?

Say I start with everything inside the body element:

Nokogiri::HTML( doc ).xpath( "/html/body/node()" ).to_html

which contains some <script> and开发者_运维技巧 <noscript>. How do I get rid of these?


You might want to change your XPath expression to:

Nokogiri::HTML( doc ).xpath( "/html/body/node()[not(self::script or self::noscript)]" ).to_html


#!/usr/bin/env ruby

require 'nokogiri'

html = <<EOT
<html>
  <head>
    <script>
      <!-- dummy script !>
    </script>
  </head>
  <body>
    <script><!-- dummy script !></script>
    <noscript>dummy script</noscript>
  </body>
</head>
EOT

doc = Nokogiri::HTML(html)

Here's the gist of it:

doc.at('body').search('script,noscript').remove

puts doc.to_xml

>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
>> <html>
>> <head>
>> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
>> <script>
>>       <!-- dummy script !>
>>     </script>
>> </head>
>> <body>
>>     
>>   </body>
>> </html>

For simplicity, I'm using Nokogiri's ability to use CSS accessors, rather than XPath.

doc.at('body').search('script,noscript').remove

looks for the first occurrence of the <body> tag, then looks inside for all <script> and <noscript> tags, removing them.

The gap between the resulting <body> tags are the result of the carriage returns in text nodes that trailed the actual target tags.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜