我需要用Nokogiri解析這段HTML代碼,但將「Piso en Calle Antonio Pascual」保存在一個變量中,而將「Peñiscola」保存在另一個變量中。如何使用Nokogiri從HTML解析多個字符串?
<h1 class="title g13_24">
Piso en Calle Antonio Pascual
<span class="title-extra-info">Peñíscola</span>
</h1>
我需要用Nokogiri解析這段HTML代碼,但將「Piso en Calle Antonio Pascual」保存在一個變量中,而將「Peñiscola」保存在另一個變量中。如何使用Nokogiri從HTML解析多個字符串?
<h1 class="title g13_24">
Piso en Calle Antonio Pascual
<span class="title-extra-info">Peñíscola</span>
</h1>
require 'nokogiri'
doc = Nokogiri::HTML.parse(<<-HTML)
<h1 class="title g13_24">
Piso en Calle Antonio Pascual
<span class="title-extra-info">Peñíscola</span>
</h1>
HTML
h1 = doc.at_css('h1.title')
str1 = h1.children[0].text.strip
# => "Piso en Calle Antonio Pascual"
str2 = h1.at_css('.title-extra-info').text.strip
# => "Peñíscola"
但坦白地說,在引入nokogiri文檔會告訴你同樣的。
對於記錄,任何格式的添加都會導致此問題中斷,而我的方法將不受影響。 – pguardiario 2012-02-22 00:29:19
@pguardiario:你的意思是什麼樣的變化? – 2012-02-22 00:37:28
對第一行的任何小改動 - 粗體,斜體,錨點等。如果你想生存下來,你必須按照我的方式來做。這不是玩笑:P – pguardiario 2012-02-22 00:46:21
不要忘記[接受答案](http://meta.stackexchange.com/a/5235/176476):) – 2012-02-28 02:52:21