Nokogiri text node contents

前端 未结 2 1997
感动是毒
感动是毒 2021-01-05 00:45

Is there any clean way to get the contents of text nodes with Nokogiri? Right now I\'m using

some_node.at_xpath( \"//whatever\" ).first.content
2条回答
  •  别那么骄傲
    2021-01-05 00:53

    You want only the text?

    doc.search('//text()').map(&:text)
    

    Maybe you don't want all the whitespace and noise. If you want only the text nodes containing a word character,

    doc.search('//text()').map(&:text).delete_if{|x| x !~ /\w/}
    

    Edit: It appears you only wanted the text content of a single node:

    some_node.at_xpath( "//whatever" ).text
    

提交回复
热议问题