【发布时间】:2018-06-17 05:32:21
【问题描述】:
我正在使用bs4 对某些文本进行一些处理,但在某些情况下它会将  字符转换为Â。我能说的最好的是这是encoding mismatch from UTF-8 to latin1(或相反?)
我的网络应用程序中的所有内容都是 UTF-8,Python3 是 UTF-8,我已经确认数据库是 UTF-8。
我已将问题缩小到这一行:
print("Before soup: " + text) # Before soup:
soup = BeautifulSoup(text, "html.parser")
#.... do stuff to soup, but all commented out for this testing.
soup = BeautifulSoup(soup.renderContents(), "html.parser") # <---- PROBLEM!
print(soup.renderContents()) # b'\xc3\x82\xc2\xa0'
print("After SOUP: " + str(soup)) # After SOUP: Â
如何防止 renderContents() 更改编码?这个函数上有no documentation!
编辑:在进一步研究文档后,this seems to be the key,但我仍然无法解决问题!
print(soup.prettify(formatter="html")) # Â
【问题讨论】:
-
stackoverflow.com/a/25871885/874188 看起来太相似了,纯属巧合,
标签: python python-3.x utf-8 beautifulsoup