【问题标题】:Cannot write German characters scraped from XPath to CSV file无法将从 XPath 抓取的德语字符写入 CSV 文件
【发布时间】:2020-05-14 16:15:23
【问题描述】:

我正在尝试将包含德语变音符号的信息写入 CSV。当我只写第一个参数“名称”时,它会正确显示。如果我写“名称”和“机构”,我会收到这个错误:

UnicodeEncodeError: 'charmap' codec can't encode character '\u0308' in position 71: character maps to <undefined>

正如您在下面的代码中看到的,我尝试使用不同的字符组合对文本进行编码和解码。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

# this is the header of the csv
with open('/filepath/result.csv', 'w', encoding='utf-8') as f:
  f.write("name, institution, \n")

l = list(range(1148, 1153))

for i in l:
    url = 'webaddress.com' + str(i)
    driver.get(url)
    name = driver.find_elements_by_xpath('//div[@style="width:600px; display:inline-block;"]')[0].text
    name = '\"' + name + '\"'
    institution = driver.find_elements_by_xpath('//div[@style="width:600px; display:inline-block;"]')[1].text
    institution = '\"' + institution + '\"'
    print(str(i) + ': ' + name, '\n', str(i) + ': ' + institution, '\n')
    print(institution.encode('utf-8'))
    print(institution.encode('utf-8').decode('utf-8'))
    print(institution.encode('utf-8').decode('ISO-8859-15'))
    with open('/filepath/result.csv', 'a', encoding='utf-8') as f:
        f.write(name + ',' + institution + '\n')

driver.close()

当我将所有编码设置为 UTF-8 时,CSV 中显示的结果与我编码 UTF-8 并解码 ISO-8859-15 (latin1) 的结果类似。当我编码 latin1 并解码 utf-8 时,我得到了与上面相同的错误。

感谢您的帮助。

【问题讨论】:

    标签: python selenium web-scraping utf-8 data-cleaning


    【解决方案1】:

    foo.py 文件顶部的行添加为:

    # -*- coding: UTF-8 -*-
    

    作为替代方案,您可以使用io 模块,如下所示:

    import io
    
    # this is the header of the csv
    with io.open('/filepath/result.csv', 'w', encoding='utf-8') as f:
      f.write("name, institution, \n")
    

    及以后:

    with io.open('/filepath/result.csv', 'a', encoding='utf-8') as f:
        f.write((name + ',' + institution + '\n')..encode("utf-8"))
    

    【讨论】:

    • 这不起作用。它写入 CSV 但不正确。
    • @JustinBurack 查看更新的答案并让我知道状态。
    • 将 .encode("utf-8") 添加到 f.write 行显示错误:write() 参数必须是 str,而不是 bytes
    【解决方案2】:

    您似乎对encode 的用途感到困惑。你为什么要print(institution.encode('utf-8').decode('utf-8'));这完全等同于print(institution)

    我猜你的回溯来自prints 之一,而不是write()。尝试取出有问题的人;或者简单地弄清楚如何将 Unicode 打印到您的控制台,然后就这样做。

    可能读过 Ned Batchelder 的 Pragmatic Unicode.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2014-02-11
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多