2013-08-27 29 views
1

我試圖打消這個錨鏈接的52:編碼:: UndefinedConversionError:U + 00A0從UTF-8到US-ASCII

<div class="zg_usedPrice"> 
<a href="http://rads.stackoverflow.com/amzn/click/B000O3GCFU">52&nbsp;new</a> 
</div> 

有了這個代碼:

def self.parse_products 
    product_hash = {} 
    product = @data.css('#zg_centerListWrapper') 

    product.css('.zg_itemImmersion').each do | product | 
     product_name = product.css('.zg_title a').text 
     product_used_price_status = product.css('.zg_usedPrice > a').text[/(\D+)/] 
     product_hash[:product] ||= [] 
     product_hash[:product] << { :name    => product_name, 
            :used_status  => product_used_price_status } 
    end 

    product_hash 
    end 

但我認爲http://www.amazon.com/gp/offer-listing/B000O3GCFU/ref=zg_bs_baby-products_price?ie=UTF8&condition=new部分在URL中產生以下錯誤:

Encoding::UndefinedConversionError: 
    U+00A0 from UTF-8 to US-ASCII 
# ./parser_spec.rb:175:in `block (2 levels) in <top (required)>' 

我想他們在「建議「,但我仍然遇到同樣的問題。有沒有解決方法?


完整的錯誤跟蹤:

1) Product (Baby) should return correct keys 
    Failure/Error: expect(product_hash[:product]["Pet Supplies"].keys).to eq(["Birds", "Cats", "Dogs", "Fish & Aquatic Pets", "Horses", "Insects", "Reptiles & Amphibians", "Small Animals"]) 
    TypeError: 
     can't convert String into Integer 
    # ./parser_spec.rb:179:in `[]' 
    # ./parser_spec.rb:179:in `block (2 levels) in <top (required)>' 

    2) Product (Baby) should return correct values 
    Failure/Error: expect(product_hash[:product]["Pet Supplies"].values).to eq([16281, 245512, 513926, 46811, 14805, 364, 5816, 19769]) 
    TypeError: 
     can't convert String into Integer 
    # ./parser_spec.rb:183:in `[]' 
    # ./parser_spec.rb:183:in `block (2 levels) in <top (required)>' 

    3) Product (Baby) should return correct hash 
    Failure/Error: expect(product_hash[:product]).to eq({"Pet Supplies"=>{"Birds"=>16281, "Cats"=>245512, "Dogs"=>513926, "Fish & Aquatic Pets"=>46811, "Horses"=>14805, "Insects"=>364, "Reptiles & Amphibians"=>5816, "Small Animals"=>19769}}) 
    Encoding::UndefinedConversionError: 
     U+00A0 from UTF-8 to US-ASCII 
    # ./parser_spec.rb:187:in `block (2 levels) in <top (required)>' 
+0

你可以給完整的錯誤跟蹤? –

+0

@Babai好的,我加了。看我的**編輯**。 – alexchenco

+0

什麼是179號線和183號線? –

回答

0

你的HTML樣品不你展示的代碼相匹配,再加上你給的網址不存在了,所以這是很難幫助你。

這裏是一個開始:

require 'nokogiri' 

html = '<div class="zg_usedPrice"> 
    <a href="http://rads.stackoverflow.com/amzn/click/B000O3GCFU">52&nbsp;new</a> 
    </div> 
' 
doc = Nokogiri::HTML(html) 

text = doc.at('div.zg_usedPrice a').text # => "52\u00A0new" 
text.gsub(/\u00A0/, ' ') # => "52 new"