【发布时间】:2019-05-07 07:40:57
【问题描述】:
我需要将 BeautifulSoup 结果保存到.txt 文件。我需要将结果转换为带有str() 的字符串,但由于列表是 UTF-8 而不起作用:
# -*- coding: utf-8 -*-
page_content = soup(page.content, "lxml")
links = page_content.select('h3', class_="LC20lb")
for link in links:
with open("results.txt", 'a') as file:
file.write(str(link) + "\n")
并得到这个错误:
File "C:\Users\omido\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 183-186: character maps to <undefined>
【问题讨论】:
标签: python python-3.x utf-8