Nokogiri 文本节点内容 [英] Nokogiri text node contents

查看:50
本文介绍了Nokogiri 文本节点内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何干净的方法可以使用 Nokogiri 获取文本节点的内容?现在我正在使用

Is there any clean way to get the contents of text nodes with Nokogiri? Right now I'm using

some_node.at_xpath( "//whatever" ).first.content

对于仅获取文本而言,这似乎非常冗长.

which seems really verbose for just getting text.

推荐答案

你想要文本?

doc.search('//text()').map(&:text)

也许你不想要所有的空白和噪音.如果只想要包含单词字符的文本节点,

Maybe you don't want all the whitespace and noise. If you want only the text nodes containing a word character,

doc.search('//text()').map(&:text).delete_if{|x| x !~ /\w/}

看来您只想要单个节点的文本内容:

It appears you only wanted the text content of a single node:

some_node.at_xpath( "//whatever" ).text

这篇关于Nokogiri 文本节点内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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