现在这是我的HTML:
<div class="cardtextbox"><i>(<img src="/Handlers/Image.ashx?size=small&name=BR&type=symbol" alt="Black or Red" align="absbottom" /> can be paid with either <img src="/Handlers/Image.ashx?size=small&name=B&type=symbol" alt="Black" align="absbottom" /> or <img src="/Handlers/Image.ashx?size=small&name=R&type=symbol" alt="Red" align="absbottom" />.)</i></div><div class="cardtextbox"><img src="/Handlers/Image.ashx?size=small&name=3&type=symbol" alt="3" align="absbottom" /><img src="/Handlers/Image.ashx?size=small&name=B&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&name=3&type=symbol" alt="3" align="absbottom" /><img src="/Handlers/Image.ashx?size=small&name=R&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
属性。
答案 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."]