Ruby 编码::未定义的转换器错误:U+;00A0从UTF-8到US-ASCII

Ruby 编码::未定义的转换器错误:U+;00A0从UTF-8到US-ASCII,ruby,encoding,utf-8,nokogiri,Ruby,Encoding,Utf 8,Nokogiri,我正在尝试取消锚链接之间的52: 我尝试了他们在“”中的建议,但我仍然遇到同样的问题。有什么解决办法吗 完整错误跟踪: 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", "

我正在尝试取消锚链接之间的52:

我尝试了他们在“”中的建议,但我仍然遇到同样的问题。有什么解决办法吗


完整错误跟踪:

  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)>'
1)产品(婴儿)应返回正确的钥匙
失败/错误:expect(product_hash[:product][“宠物用品”].键)。到eq([“鸟”、“猫”、“狗”、“鱼和水生宠物”、“马”、“昆虫”、“爬行动物和两栖动物”、“小动物”]))
类型错误:
无法将字符串转换为整数
#./parser_spec.rb:179:in`[]'
#./parser_spec.rb:179:in'block(2层)in'
2) 产品(婴儿)应返回正确的值
失败/错误:expect(product_hash[:product][“Pet Supplies”].值)。到eq([162812455135264681114805364581619769])
类型错误:
无法将字符串转换为整数
#./parser_spec.rb:183:in`[]'
#./parser_spec.rb:183:in'block(2层)in'
3) 产品(婴儿)应返回正确的哈希值
失败/错误:预期(产品散列[:产品])到eq({“宠物用品”=>16281,“猫”=>245512,“狗”=>513926,“鱼和水生宠物”=>46811,“马”=>14805,“昆虫”=>364,“爬行动物和两栖动物”=>5816,“小动物”=>19769})
编码::未定义的转换器错误:
从UTF-8到US-ASCII的U+00A0
#./parser_spec.rb:187:in'block(2层)in'

您的HTML示例与显示的代码不匹配,加上您提供的URL已不存在,因此很难为您提供帮助

这是一个开始:

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"
需要“nokogiri”
html='1〕
'
doc=Nokogiri::HTML(HTML)
text=doc.at('div.zg_usedPrice a')。text=>“52\u00A0new”
text.gsub(/\u00A0/,'')#=>“52新”

您能提供完整的错误跟踪吗?@Babai好的,我添加了它。请参阅我的编辑。第179行和第183行是什么?@Babai行179:`product_hash[:product]product_asin,`@Babai行183`:used_status=>product_used_price_status`
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)>'
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"