Nokogiri 以字符串形式返回值,而不是数组 [英] Nokogiri returning values as a string, not an array

查看:44
本文介绍了Nokogiri 以字符串形式返回值,而不是数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nokogiri 运行一个返回多个值的脚本.我的印象(并得到多个来源的保证)结果应该是数组的形式.相反,我得到了一个难看的字符串.这是代码

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 an array. Instead I'm getting an ugly looking string. Here is the code

require 'nokogiri'
require 'open-uri'
require 'spreadsheet'

profile_page_scraper = Nokogiri::HTML(open('http://www.crunchbase.com/company/facebook'))       
puts profile_page_scraper.css('div.col1_content td.td_left').text

返回这个:

PublicDateRaisedPost IPO ValuationWebsiteBlogTwitterCategoryEmployeesFoundedDescription

我知道我可以使用 map 来快速解决这个问题,但我很困惑为什么这不返回数组.从理论上讲,它应该返回如下内容:

I know I can use map to fix this quickly, but I am confused as to why this isn't returning an array. It should, theoretically, return something like this:

["Public", "Date", "Raised" ... "Description"]

任何想法为什么这不起作用?

Any ideas why this isn't working?

推荐答案

NodeSet#text 总是返回一个字符串(否则它可能被称为 NodeSet#texts).Nokogiri 文档不是很好,如有疑问,请查看源代码:

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

获取文本数组:nodes.map(&:text)

这篇关于Nokogiri 以字符串形式返回值,而不是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆