Nokogiri returning values as a string, not an array

故事扮演 提交于 2019-12-01 21:57:10

NodeSet#text always returns a string (otherwise it would probably be called NodeSet#texts). Nokogiri docs are not so great, when in doubt check the source code:

  # lib/nokogiri/xml/node_set.rb
  def inner_text
    collect{|j| j.inner_text}.join('')
  end
  alias :text :inner_text

To get an array of texts: nodes.map(&:text)

The css method is returning an instance of Nokogiri::XML::NodeSet, which includes Enumerable.

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open('http://www.crunchbase.com/company/facebook'))  
nodes = doc.css('div.col1_content td.td_left')
nodes.class.ancestors
# => [Nokogiri::XML::NodeSet, Enumerable, Object, Kernel, BasicObject]

So you can use all the standard iterators in conjunction with the content attribute of each element in this result set. For example:

nodes.each { |n| puts n.content }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!