解析nokogiri元素时出现问题



我使用Mechanize gem来解析html内容。

在终端上执行此操作,

 agent.get("http://www.example.com/").search(".sidebar-deal-excerpt").first

给了我这个:

 <Nokogiri::XML::Element:0x3fcdb3add700 name="div" attributes=[#<Nokogiri::XML::Attr:0x3fcdb3add570 name="class" value="sidebar-deal-excerpt">] children=[#<Nokogiri::XML::Text:0x3fcdb3add0ac "rnttttttttttttt">, #<Nokogiri::XML::Element:0x3fcdb3adcfd0 name="a" attributes=[#<Nokogiri::XML::Attr:0x3fcdb3adcf6c name="href" value="http://www.example.com/getaway-8774-yatra-september-deal">] children=[#<Nokogiri::XML::Text:0x3fcdb3adcaa8 " ">, #<Nokogiri::XML::Element:0x3fcdb3adc9cc name="span" attributes=[#<Nokogiri::XML::Attr:0x3fcdb3adc968 name="style" value="color: #000">] children=[#<Nokogiri::XML::Text:0x3fcdb3dd6b50 " international holiday package bookings at Yatra.com ">]>, #<Nokogiri::XML::Text:0x3fcdb3dd5f5c " ">]>, #<Nokogiri::XML::Text:0x3fcdb3dd5a98 "rntttttttttttt">]> 

在终端上执行

agent.get("http://www.example.com/").search(".sidebar-deal-excerpt").first.children

给了我这个:

 <Nokogiri::XML::Text:0x3fcdb399c3dc "rnttttttttttttt">, #<Nokogiri::XML::Element:0x3fcdb399c300 name="a" attributes=[#<Nokogiri::XML::Attr:0x3fcdb399c29c name="href" value="http://www.example.com/getaway-8774-yatra-september-deal">] children=[#<Nokogiri::XML::Text:0x3fcdb399bdd8 " ">, #<Nokogiri::XML::Element:0x3fcdb399bcfc name="span" attributes=[#<Nokogiri::XML::Attr:0x3fcdb399bc98 name="style" value="color: #000">] children=[#<Nokogiri::XML::Text:0x3fcdb399b7d4 "international holiday package bookings at Yatra.com ">]>, #<Nokogiri::XML::Text:0x3fcdb3dd8770 " ">]>, #<Nokogiri::XML::Text:0x3fcdb3dd6df8 "rntttttttttttt">]

问题是,我想从这个获取url。我想要得到这个属性值

value="http://www.example.com/getaway-8774-yatra-september-deal"
ie

   http://www.example.com/getaway-8774-yatra-september-deal

我如何解析这个?

(未经测试)

agent = Mechanize.new
page = agent.get("http://www.snapdeal.com/")
page = agent.get("http://www.snapdeal.com/") # yeah, you've got to call it twice for this site
urls = page.search(".sidebar-deal-excerpt a").collect{|a| a.attributes["href"].value }

相关内容

  • 没有找到相关文章

最新更新