【发布时间】: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&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