捕获标签之间的特定文本

时间:2011-05-22 18:29:47

标签: ruby nokogiri

解释在评论中。我把它放在那里因为被解释为粗体或其他东西,它搞砸了帖子。

# I need to capture text that is
# enclosed in tags that are both <b> and
# <i>, but if there is more than one
# text enclosed in <i> in the same <b>
# block, then I only want the text
# enclosed in the first <i> tag, For
# example, for the following line:
# 
# <b> <i> Important text here </i>
# irrelevant text everywhere else <i>
# irrelevant text here </i> </b>  <b>
# <i> Also Important </i> not important
# <i> not important </i> </b> 
# 
# I want to retrieve only: 
# - Important text here 
# - Also Important
# 
# I also must not retrieve text inside an
# <h2> block. I have been trying to
# delete the block with nodes.delete(nodes. search('h2')), 
# but it doesn't actually delete the h2 block 


require "rubygems"
require "nokogiri"

html = <<EOT
  <b><i> Important text here </i> more text <i> not important text here </i> </b>
  <b> <i> Also Important </i> more text <i> not important </i> </b> 

  <h2><b> <i> I don't want this text either</i></b></h2> 
EOT


doc = Nokogiri::HTML(html)

nodes = doc.search('b i')

nodes.each { |e| puts e }

# Expected output:
# Important text here
# Also Important

1 个答案:

答案 0 :(得分:0)

require "nokogiri"
require 'pp'
html = <<EOT
  <b><i>Important text here</i>more text<i>not important text here</i></b>
  <b><i>Also Important</i>more text<i>not important</i></b> 

  <h2><b><i>I don't want this text either</i></b></h2> 
EOT


doc = Nokogiri::HTML(html)
nodes = doc.search('b')
nodes.each { |e| puts e.children.children.first unless e.parent.name == "h2" }

或使用xpath:

nodes = doc.xpath("//../*[local-name() != 'h2']/b/i[1]")
nodes.each { |e| puts e.children.first}