我正在使用 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('www.crunchbase/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 以字符串形式返回值,而不是数组
发布评论