我有这样的例子:
html= <<EOT
<div>Some text1
<p>Some text2</p>
</div>
EOT
doc = Nokogiri::HTML(html)
puts doc.css('div').text
这使得:
Some text1
Some text2
但是我只需要"Some text1"
doc.css('div').children.first.text
# => "Some text1n "
doc.css('div').children.first.text.rstrip
# => "Some text1"
一个XPath表达式和一个strip
就可以实现:
some_text1 = doc.xpath('//div/text()[1]').text.strip