John Doe
John Doe

Reputation: 87

How to get "href" from tag

I am trying to get "href" but can't.

I have this code:

<h3 class="list-item-hd">
    <a href="/music/news/dwight-yoakam-and-jack-black-to-produce-new-nashville-comedy-20151009">Dwight Yoakam and Jack Black to Produce New Nashville Comedy</a>
</h3>

How can I get?

/music/news/dwight-yoakam-and-jack-black-to-produce-new-nashville-comedy-20151009

Upvotes: 2

Views: 189

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34336

This code is using a CSS selector to grab all the anchor elements and print their href attributes:

require 'nokogiri'

html = <<EOT
<html>
    <h3 class="list-item-hd">
       <a href="/music/news/dwight-yoakam-and-jack-black-to-produce-new-nashville-comedy-20151009">Dwight Yoakam and Jack Black to Produce New Nashville Comedy</a>
   </h3> 
</html>
EOT

doc = Nokogiri::HTML(html)
doc.css('a').map { |link| p link['href'] }
# => "/music/news/dwight-yoakam-and-jack-black-to-produce-new-nashville-comedy-20151009"

You can achieve your goal by either using a CSS or XPath selector. To learn more about CSS and XPath selectors, I highly recommend you to take a look at the "Official Nokogiri Documentation for Searching a XML/HTML Document."

Upvotes: 1

Related Questions