如何从标签中获取 "href"

How to get "href" from tag

我正在尝试 "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

此代码使用 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 选择器的更多信息,我强烈建议您查看“Official Nokogiri Documentation for Searching a XML/HTML Document.