2012-06-10 34 views
0

提取從字符串IMG源鏈接我有這個字符串紅寶石

#<Fletcher::Model::Amazon alt="You Are Not a Gadget: A Manifesto (Vintage)" border="0" element="img" height="240" id="prodImage" onload="if (typeof uet == 'function') { if(typeof setCSMReq=='function'){setCSMReq('af');setCSMReq('cf');}else{uet('af');uet('cf');amznJQ.completedStage('amznJQ.AboveTheFold');} }" onmouseout="sitb_doHide('bookpopover'); return false;" onmouseover="sitb_showLayer('bookpopover'); return false;" src="http://ecx.images-amazon.com/images/I/51bpl1wA%2BaL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg" width="240"> 

我只是想在src屬性的鏈接:

http://ecx.images-amazon.com/images/I/51bpl1wA%2BaL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg" 

我如何解析這個字符串來獲得鏈接

下面是相關功能的列表

module Fletcher 
    module Model 
    class Amazon < Fletcher::Model::Base 
     # A regular expression for determining if a url comes from a specific service/website 
     def self.regexp 
     /amazon\.com/ 
     end 

     # Parse data and look for object attributes to give to object  
     def parse(data) 
     super(data) 

     case doc 
     when Nokogiri::HTML::Document 
      # Get Name 
      self.name = doc.css("h1.parseasinTitle").first_string 

      # Get Description 
      self.description = doc.css("div#productDescriptionWrapper").first_string  

      # Get description from meta title if not found 
      self.description = doc.xpath("//meta[@name='description']/@content").first_string if description.nil? 

      # Get Price 
      parse_price(doc.css("b.priceLarge").first_string) 

      # Get Images 
      self.images = doc.xpath("//table[@class='productImageGrid']//img").attribute_array 
      self.image = images.first 
     end    
     end 
    end 
    end 
end 
+1

你的字符串看起來很像是調用'Ruby對象上inspect'的輸出;你有實際的物體嗎? –

+0

@AndrewMarshall我不完全確定你指的是什麼「實際」的對象。整個寶石(fletcher)在github上。 https://github.com/hulihanapplications/fletcher。我已經包含了上面的類和方法。 –

+0

那麼,你是如何得到這個字符串的? –

回答

1

在這種情況下,我相信這將是:fletchedProduct.image [:SRC]

+0

完美運作 –

1
require 'open-uri' 

x = %Q{#<Fletcher::Model::Amazon alt="You Are Not a Gadget: A Manifesto (Vintage)" border="0" element="img" height="240" id="prodImage" onload="if (typeof uet == 'function') { if(typeof setCSMReq=='function'){setCSMReq('af');setCSMReq('cf');}else{uet('af');uet('cf');amznJQ.completedStage('amznJQ.AboveTheFold');} }" onmouseout="sitb_doHide('bookpopover'); return false;" onmouseover="sitb_showLayer('bookpopover'); return false;" src="http://ecx.images-amazon.com/images/I/51bpl1wA%2BaL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg" width="240">} 

url = URI.extract(x) 

puts url[2] 

輸出:

http://ecx.images-amazon.com/images/I/51bpl1wA%2BaL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg 

希望這有助於。我恰巧需要能夠在上週做到這一點,並查找它。