转换为utf16

2024-06-26 14:29:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我爬了几个网站,并提取产品的名称。某些名称中存在如下错误:

Malecon 12 Jahre 0,05 ltr.<br>Reserva Superior
Bols Watermelon Lik\u00f6r 0,7l
Hayman\u00b4s Sloe Gin
Ron Zacapa Edici\u00f3n Negra
Havana Club A\u00f1ejo Especial
Caol Ila 13 Jahre (G&amp;M Discovery)

我怎样才能解决这个问题? 我正在使用xpath和re.search获取名称

在每个Python文件中,这是第一个代码:# -*- coding: utf-8 -*-

编辑:

这是源代码,我是如何获得信息的

if '"articleName":' in details:
                            closer_to_product = details.split('"articleName":', 1)[1]
                            closer_to_product_2 = closer_to_product.split('"imageTitle', 1)[0]
                            if debug_product == 1:
                                print('product before try:' + repr(closer_to_product_2))
                            try:
                                found_product = re.search(f'{'"'}(.*?)'f'{'",'}'closer_to_product_2).group(1)
                            except AttributeError:
                                found_product = ''
                            if debug_product == 1:
                                print('cleared product: ', '>>>' + repr(found_product) + '<<<')
                            if not found_product:
                                print(product_detail_page, found_product)
                                items['products'] = 'default'
                            else:
                                items['products'] = found_product

细节

product_details = information.xpath('/*').extract()
product_details = [details.strip() for details in product_details]

Tags: toinre名称searchifproductdetails
1条回答
网友
1楼 · 发布于 2024-06-26 14:29:16

问题在哪里(Python 3.8.3)

import html

strings = [
  'Bols Watermelon Lik\u00f6r 0,7l',
  'Hayman\u00b4s Sloe Gin',
  'Ron Zacapa Edici\u00f3n Negra',
  'Havana Club A\u00f1ejo Especial',
  'Caol Ila 13 Jahre (G&amp;M Discovery)',
  'Old Pulteney \\u00b7 12 Years \\u00b7 40% vol',
  'Killepitsch Kr\\u00e4uterlik\\u00f6r 42% 0,7 L']
  
for str in strings:
  print( html.unescape(str).
                encode('raw_unicode_escape').
                decode('unicode_escape') )
Bols Watermelon Likör 0,7l
Hayman´s Sloe Gin
Ron Zacapa Edición Negra
Havana Club Añejo Especial
Caol Ila 13 Jahre (G&M Discovery)
Old Pulteney · 12 Years · 40% vol
Killepitsch Kräuterlikör 42% 0,7 L

编辑使用.encode('raw_unicode_escape').decode('unicode_escape')作为双面反面索利多金币,请参见Python Specific Encodings

相关问题 更多 >