0

私はこの文字列を持っています

#<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
4

2 に答える 2

1

その場合、次のようになると思います:fletchedProduct.image [:src]

于 2012-06-10T22:17:40.023 に答える
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

お役に立てれば。たまたま先週これを行う必要があり、調べました。

于 2012-06-10T21:45:10.293 に答える