【问题标题】:Convert in utf16转换成 utf16
【发布时间】:2020-10-30 10:46:27
【问题描述】:

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

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]

【问题讨论】:

  • 这取决于您使用的是什么。 :) 也许.encode('utf-8') 会做
  • 这能回答你的问题吗? How to convert a string to utf-8 in Python
  • 这显然不是 UFT-8。任何包含 \u00 的 unicode 序列都是无效的 UTF-8。肯定是 UTF-16-BE
  • 好的,转储问题,但是要# -- coding: utf-16 -- help?
  • 这个没用 "# -- coding: utf-8 --" 这严格来说只是为了源代码的编码,执行代码时没有效果(它是默认设置,如果 Python 没有找到强提示,则编码方式不同(例如 BOM)。如果您的代码不是 UTF-16,请不要使用 UTF-16(可能没有人会使用 UTF16 作为代码。第二点 \uxxxx与 UTF16 无关,它只是 unicode 代码点的表示,与编码无关。

标签: python html utf-8 python-unicode


【解决方案1】:

哪里出了问题(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') 进行加倍Reverse Solidi,参见Python Specific Encodings

【讨论】:

  • 此解决方案有助于解决某些问题。但我仍然得到这样的输出:Old Pulteney \\u00b7 12 Years \\u00b7 40% vol 或这个Killepitsch Kr\\u00e4uterlik\\u00f6r 42% 0,7 L 这是因为双反斜杠吗?
  • @CIC3RO 答案已更新为 double 反斜杠,请参阅 strings[-2:]...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 2012-02-13
  • 1970-01-01
  • 2017-08-22
  • 2013-05-29
  • 2012-11-04
相关资源
最近更新 更多