ToTheMoon
ToTheMoon

Reputation: 89

How to scarpe the href using Nokogiri

I have a variable e which stores a Nokogiri::XML::Element object.

when I execute puts e I get on the screen the following:

<h3 class="fixed-recipe-card__h3">
        <a href="https://www.allrecipes.com/recipe/21712/chocolate-covered-strawberries/" data-content-provider-id="" data-internal-referrer-link="hub recipe" class="fixed-recipe-card__title-link">
            <span class="fixed-recipe-card__title-link">Chocolate Covered Strawberries</span>
        </a>
    </h3>

I would like to scrape this part https://www.allrecipes.com/recipe/21712/chocolate-covered-strawberries/

How can I do this using Nokogiri

Upvotes: 1

Views: 62

Answers (1)

max pleaner
max pleaner

Reputation: 26788

If you want to extract the link, you can use:

e.at_css("a").attributes["href"].value

.at_css returns the first element matching the CSS selector (another Nokogiri::XML::Element). To get a list of all matching elements, use .css instead.

.attributes gives you a hash mapping attribute name to Nokogiri::XML::Attr. Once you look up the desired attribute in this hash (href), you can call .value to get the actual text value.

Upvotes: 1

Related Questions