Get text directly inside a tag in Nokogiri

Mridang Agarwalla picture Mridang Agarwalla · May 29, 2012 · Viewed 16.6k times · Source

I have some HTML that looks like:

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

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.

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.

Answer

Casper picture Casper · May 29, 2012

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()')