Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract the child text with Nokogiri?

I encountered this HTML:

<div class='featured'>
    <h1>
        How to extract this?
        <span>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
        <span class="moredetail ">
            <a href="/hello" title="hello">hello</a>
        </span>
        <div class="clear"></div>
    </h1>
</div>

I want to extract the <h1> text "How to extract this?". How do I do so?

I tried with the following code, but there's other element appended. I am not sure how to exclude them so I get only the <h1> text itself.

doc = Nokogiri::HTML(open(url))      
records = doc.css(".featured h1")
like image 499
TonyTakeshi Avatar asked Dec 18 '11 04:12

TonyTakeshi


1 Answers

#css returns a collection, use #at_css to get the first matching node. All its contents, even text, are children, and in this case, the text is its first child. You could also do something like children.reject &element? if you wanted all the children that weren't elements.

data = '
<div class="featured">
    <h1>
        How to extract this?
        <span>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
        <span class="moredetail ">
            <a href="/hello" title="hello">hello</a>
        </span>
        <div class="clear"></div>
    </h1>
</div>
'

require 'nokogiri'
text = Nokogiri::HTML(data).at_css('.featured h1').children.first.text
text # => "\n        How to extract this?\n        "

Alternatively, you can use xpaths:

Nokogiri::HTML(data).at_xpath('//*[@class="featured"]/h1/text()').text
like image 155
Joshua Cheek Avatar answered Oct 22 '22 18:10

Joshua Cheek