如何从标签中获取“href”

时间:2015-10-12 13:02:37

标签: ruby ruby-on-rails-4 rubygems nokogiri

我想获得“href”但不能。

我有这段代码:

<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>

我怎么才能得到?

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

1 个答案:

答案 0 :(得分:1)

此代码使用CSS选择器来获取所有锚元素并打印其href属性:

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"

您可以使用CSS或XPath选择器来实现目标。要了解有关CSS和XPath选择器的更多信息,我强烈建议您查看&#34; Official Nokogiri Documentation for Searching a XML/HTML Document.&#34;