gpt4 book ai didi

ruby - 从 Mechanize/Nokogiri 获取链接

转载 作者:数据小太阳 更新时间:2023-10-29 08:51:41 26 4
gpt4 key购买 nike

我正在尝试发现从 Nokogiri 节点检索 href 链接的最佳方法。这是我所在的地方

mech = Mechanize.new 
mech.get(HOME_URL)

mech.page.search('.listing_content').each do |business|
website = business.css('.website-feature')
puts website.class
puts website.inner_html
end

输出=>

Nokogiri::XML::NodeSet<a href="http://urlofsite.com" class="track-visit-website no-tracks"  onclick='omniture.callClick({"eVar6":6,"eVar9":1,"eVar21":"search_results","eVar50":null,"prop17":"cars","prop26":"64c15af0-a558-012f-a041-00215a4685f6","eVar42":"64c15af0-a558-012f-a041-00215a4685f6","prop27":6,"prop38":"search_results","prop39":1,"prop46":null,"events":"event6,event7","eVar51":optimostIDs.trialID.toString(),"eVar52":optimostIDs.segmentID.toString(),"eVar53":optimostIDs.creativeID.toString(),"eVar54":optimostIDs.subjectID.toString(),"prop47":null,"prop51":optimostIDs.trialID.toString(),"prop52":optimostIDs.segmentID.toString(),"prop53":optimostIDs.creativeID.toString(),"prop54":optimostIDs.subjectID.toString(),"prop56":"Saint+George%2C+UT","prop57":null,"prop58":false,"prop59":null,"eVar60":"relevancyTest2","prop60":"relevancyTest2","prop61":false,"prop62":null,"prop64":null,"prop67":null,"prop68":null,"prop70":null,"prop71":null});; atti_logs.attiClick({"iid":"651691e0-a558-012f-2ca7-18a9053c171a","lt":6,"ptid":"www.yellowpages.com","rid":"vendetta-236e7298-3a4f-4744-8ff5-4eb5fcc8e188","ypid":3848879,"lid":3848879,"vrid":"64c15af0-a558-012f-a041-00215a4685f6","nav":null});' rel="nofollow" target="_blank" title="Executive Service Ctr Website"><span class="raquo">»</span>  Website</a>

基本上,我只需要从 inner_html 中获取 http://urlofsite.com,但我不确定该怎么做。我读过有关使用 CSS 和 XPATH 进行此操作的信息,但目前我无法使用它们。感谢您的帮助

最佳答案

首先,告诉 Nokogiri 获取一个节点,而不是一个 NodeSet。 at_css将检索节点和 css检索一个类似于数组的 NodeSet。

代替:

website = business.css('.website-feature')

尝试:

website = at_css('a.track-visit-website no-tracks')

检索 <a> 的第一个实例节点 class="website-feature" .如果它不是您想要的第一个实例,那么您需要通过获取 NodeSet 然后对其进行索引来缩小它的范围。如果没有周围的 HTML,就很难提供更多帮助。

获取href来自节点的参数,只需将节点视为散列即可:

website['href']

应该返回:

http://urlofsite.com

这是来自 IRB 的一个小样本:

irb(main):001:0> require 'nokogiri'
=> true
irb(main):002:0>
irb(main):003:0* html = '<a class="this_node" href="http://example.com">'
=> "<a class=\"this_node\" href=\"http://example.com\">"
irb(main):004:0> doc = Nokogiri::HTML.parse(html)
=> #<Nokogiri::HTML::Document:0x8041e2ec name="document" children=[#<Nokogiri::XML::DTD:0x8041d20c name="html">, #<Nokogiri::XML::Element:0x805a2a14 name="html" children=[#<Nokogiri::XML::Element:0x805df8b0 name="body" children=[#<Nokogiri::XML::Element:0x8084c5d0 name="a" attributes=[#<Nokogiri::XML::Attr:0x80860170 name="class" value="this_node">, #<Nokogiri::XML::Attr:0x8086047c name="href" value="http://example.com">]>]>]>]>
irb(main):005:0>
irb(main):006:0* doc.at_css('a.this_node')['href']
=> "http://example.com"
irb(main):007:0>

关于ruby - 从 Mechanize/Nokogiri 获取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11279303/

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