Python3返回意外的Unicode / ASCII

问题描述 投票:2回答:1

环境:Python 3.6,bs4

我正在使用以下代码从本地html文件中“抓取”数据。该代码通常按预期返回数据,除了我的代码返回ü而不是返回ü

我正在将恢复的数据保存到UTF-8 .csv文件。 ü字符正显示在控制台和.csv文件中。我不确定问题出在哪里。这是Unicode /字符映射问题吗?我的密码?还是其他?

我希望返回正确的字符,在这种情况下为ü

我的代码段...

#   https://stackoverflow.com/questions/50308840/error-extracting-text-from-website-attributeerror-nonetype-object-has-no-attr
    try:
        description_elemt = result.find("div", attrs = {'class':'product-desc'}).get_text(strip=True)
    except:
        traceback.print_exc()  #  debug info to console only
        description_elemt = 'No data given'
    #   replace 'Length' with 'length', this leaves 'Blade length: xxx mm' as one cell/element
    #   ref chapter 2.5, p45, Python Cookbook, 9781449340377
    description_elemt = description_elemt.replace('Length', 'length')
    description_elem = re.findall('[A-Z][^A-Z]*', description_elemt)
    #   gather the line items into one row ,separated by ,
    description_elem = ','.join(description_elem)```

Thanks guys.
python python-3.x web-scraping
1个回答
0
投票

我在您的示例中看不到任何在控制台上显示文本的内容。您也不应使用','.join(description_elem)构造CSV行。请注意,Unicode字符ü被编码为[UTF-8]。在ISO 8859-13中,编码\xC3\xBC\xC3Ã\xBC。其他ISO 8859编码共享这些代码点到字形的映射中的一个或两个。这说明我正在解析的输入不是UTF-8,您的程序未使用UTF-8编码,或者您正在使用非UTF-8语言环境查看程序的输出。换句话说,问题出在您的代码或终端仿真器和/或Shell的语言环境。

© www.soinside.com 2019 - 2024. All rights reserved.