Nokogiri 以字符串形式返回值,而不是数组

编程入门 行业动态 更新时间:2024-10-28 10:30:17
本文介绍了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('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 以字符串形式返回值,而不是数组

本文发布于:2023-11-05 19:51:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   返回值   而不是   形式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!