I am trying to get the text for Last sold date
from this HTML:
<td class="browse-cell-date">
<span title="Last sold date">
May 2002
</span>
<button class="btn btn-previous-sales js-btn-previous-sales">
Previous sales (1) <i class="icon icon-down-open-1"/>
</button>
<div class="previous-sales-panel is-hidden">
<span style="display: block;">
Aug 1997
<span class="fright">£60,000</span>
</span>
</div>
</td>
I tried:
date = val.search(".//td[@class='browse-cell-date']").children[1]
It gave me the span I wanted but after adding .text
to it, did not returned anything.
I'd start with:
So
will do it.
at
is likesearch('some selector').first
so use it for convenience. Bothat
andsearch
are smart enough to figure out whether the selector is CSS or XPath most of the time so I use those. If Nokogiri is fooled I'll revert to using one of the*_css
or*_xpath
variants.Alternately you could use:
Note: Using
text
with any of thesearch
,xpath
orcss
methods isn't a good idea. Those methods return a NodeSet, which doesn't do what you expect when you use itstext
method. Consider these examples:We regularly see questions where people have done this and then need to figure out how to split the concatenated text into something useful, which usually is very difficult.
99.99% of the time, you want to use the following
map(&:text)
to extract the text from a NodeSet:But, in your use, simply use
at
, which returns a Node and thentext
will do what you expect.Try this