直接在 Nokogiri 的标签内获取文本 [英] Get text directly inside a tag in Nokogiri

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

问题描述

我有一些 HTML 看起来像:

I have some HTML that looks like:

<dt>
  <a href="#">Hello</a>
  (2009)
</dt>

我已经将所有 HTML 加载到名为 record 的变量中.如果存在,我需要解析年份,即 2009.

I already have all my HTML loaded into a variable called record. I need to parse out the year i.e. 2009 if it exists.

如何获取 dt 标签内的文本而不是 a 标签内的文本?我用过 record.search("dt").inner_text 这给了我一切.

How can I get the text inside the dt tag but not the text inside the a tag? I've used record.search("dt").inner_text and this gives me everything.

这是一个微不足道的问题,但我还没有弄清楚.

It's a trivial question but I haven't managed to figure this out.

推荐答案

要使用文本获取所有直接子级,但不包含任何进一步的子子级,您可以像这样使用 XPath:

To get all the direct children with text, but not any further sub-children, you can use XPath like so:

doc.xpath('//dt/text()')

或者如果您想使用搜索:

Or if you wish to use search:

doc.search('dt').xpath('text()')

这篇关于直接在 Nokogiri 的标签内获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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