我有这样的例子:
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 text1\n "
doc.css('div').children.first.text.rstrip
# => "Some text1"
关于ruby - Nokogiri:如何解析文本片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419496/