我想获得“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
答案 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;