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.
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.
Just look for text nodes:
Which outputs:
BTW, your code example doesn't work.
at_xpath( "//whatever" ).first
is redundant and will fail.at_xpath
will find only the first occurrence, returning a Node.first
is superfluous at that point, if it would work, but it won't because Node doesn't have afirst
method.Assuming
doc
contains the parsed DOM:Get the first occurrence:
Get all occurrences and take the first one:
You want only the text?
Maybe you don't want all the whitespace and noise. If you want only the text nodes containing a word character,
Edit: It appears you only wanted the text content of a single node: