如何使用nokogiri在文本中获取嵌入元素?

时间:2010-12-03 16:08:56

标签: nokogiri

现在这是我的HTML:

<div class="cardtextbox"><i>(<img src="/Handlers/Image.ashx?size=small&amp;name=BR&amp;type=symbol" alt="Black or Red" align="absbottom" /> can be paid with either <img src="/Handlers/Image.ashx?size=small&amp;name=B&amp;type=symbol" alt="Black" align="absbottom" /> or <img src="/Handlers/Image.ashx?size=small&amp;name=R&amp;type=symbol" alt="Red" align="absbottom" />.)</i></div><div class="cardtextbox"><img src="/Handlers/Image.ashx?size=small&amp;name=3&amp;type=symbol" alt="3" align="absbottom" /><img src="/Handlers/Image.ashx?size=small&amp;name=B&amp;type=symbol" alt="Black" align="absbottom" />, Discard a card: Target creature gets -2/-2 until end of turn.</div><div class="cardtextbox"><img src="/Handlers/Image.ashx?size=small&amp;name=3&amp;type=symbol" alt="3" align="absbottom" /><img src="/Handlers/Image.ashx?size=small&amp;name=R&amp;type=symbol" alt="Red" align="absbottom" />: Put a 2/1 red Goblin creature token with haste onto the battlefield. Exile it at the beginning of the next end step.</div></div>

我还想得到什么:

[
 ["(B/R can be paid with either B or R.)"],
 ["3 B, Discard a card", "Target creature gets -2/-2 until end of turn"],
 ["3 R",                 "Put a 2/1 red Goblin creature token with haste onto the battlefield. Exile it at the beginning of the next end step."]
]

从红色映射=&gt; R通过colorhash完成。红色来自img标记,alt属性。

1 个答案:

答案 0 :(得分:0)

我不确定colorhash是什么,或者它与这个问题的关系如何,但这里有一些可以让你接近的东西。如果你真的想要一个嵌套的答案数组,你必须定义一个递归函数来处理一个节点,并自己确定一个节点是否是一个叶子。

require 'nokogiri'
colorhash = {
  'Red'          => 'R',
  'Black'        => 'B',
  'Black or Red' => 'B/R'
}

h = Nokogiri::HTML html_from_question

# Replace all images with alt text, possibly transformed
h.xpath('//img[@alt]').each{ |i| i.swap( colorhash[i['alt']] || i['alt']) }

require 'pp'
pp h.css('.cardtextbox').map(&:text)

#=> ["(B/R can be paid with either B or R.)",
#=>  "3B, Discard a card: Target creature gets -2/-2 until end of turn.",
#=>  "3R: Put a 2/1 red Goblin creature token with haste onto the battlefield. Exile it at the beginning of the next end step."]