Nokogiri returning values as a string, not an array

前端 未结 2 1153
逝去的感伤
逝去的感伤 2021-01-20 16:05

I\'m running a script using Nokogiri that returns multiple values. I was under the impression (and reassured by multiple sources) that the results should be in the form of a

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 16:33

    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 }
    

提交回复
热议问题