BeautifulSoup Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.

BeautifulSoup很赞的东西

最近出现一个问题:Python 3.3

soup=BeautifulSoup(urllib.request.urlopen(url_path),"html.parser")

soup.findAll("a",{"href":re.compile('^http|^/')})

 出现warning:

Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.

在极少数情况下(通常当UTF-8文档包含以完全不同的编码编写的文本时),获取Unicode的唯一方法是使用特殊的Unicode字符“REPLACEMENT CHARACTER”(U + FFFD)替换某些字符。 如果是Unicode,Dammit需要这样做,它将在UnicodeDammit或BeautifulSoup对象上将.contains_replacement_characters属性设置为True。 这让您知道Unicode表示不是原始的精确表示 - 一些数据丢失。 如果文档包含 ,但是.contains_replacement_characters为False,那么您将知道 原来是存在的,并且不代表缺少的数据。

解决:soup=BeautifulSoup(urllib.request.urlopen(url_path),""html.parser",from_encoding="iso-8859-1")

原文地址:https://www.cnblogs.com/HANYI7399/p/6080070.html