gpt4 book ai didi

ruby - 如何从标签中获取 "href"

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:14 24 4
gpt4 key购买 nike

我正在尝试获取“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 选择器获取所有 anchor 元素并打印它们的 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.

关于ruby - 如何从标签中获取 "href",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33081845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com