2012-07-01 89 views
2

我在嘗試發現從Nokogiri節點檢索href鏈接的最佳方法。下面是我在從Mechanize/Nokogiri獲取鏈接

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> 

基本上在那裏,我只需要得到http://urlofsite.cominner_html的,我不知道該怎麼做。我已經閱讀了關於使用CSS和XPATH來做這件事,但是我現在無法在這一點上工作。感謝任何幫助

回答

3

首先,告訴Nokogiri得到一個節點,而不是一個NodeSet。 at_css將檢索節點,並且css檢索NodeSet,它就像一個Array。

相反的:

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

嘗試:

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

class="website-feature"來檢索<a>節點的第一個實例。如果它不是您想要的第一個實例,那麼您需要通過抓取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> 
+0

感謝您的信息。每當我嘗試用at_css('a.track-visit-website no-tracks')抓取節點時,它都會返回一個nilclass。我要編輯我的文章繼續前進,並看看 – ruevaughn

+0

經過一段時間後,我能夠完全按照你所描述的來完成它。謝謝你的幫助,錫人終究有一顆心;) – ruevaughn

+0

我很高興它的工作。 Nokogiri是一個很棒的XML/HTML解析器,所以感謝這個團隊。 –