【发布时间】:2014-12-24 02:28:37
【问题描述】:
我正在 Python 3.3 上使用 BeautifulSoup 构建一个网络爬虫
但是,我遇到了一个问题,导致我无法获得可以与 BeautifulSoup 一起使用的有效字符串 *。那就是:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 7047: invalid continuation byte
我知道有几十个类似的问题,但到目前为止我还没有找到一种方法可以帮助我诊断以下代码的问题:
import urllib.request
URL = "<url>" # sorry, I cannot show the url for privacy reasons, but it's a normal html document
page = urllib.request.urlopen(URL)
page = page.read().decode("utf-8") # from bytes to <source encodings>
我猜我注意到了这个错误只发生在某些 URL 上,而不会发生在其他 URL 上。即使有同样的错误,我直到昨天才出现这个错误。然后今天我再次运行程序,弹出错误..
关于如何诊断错误的任何线索?
【问题讨论】:
标签: python unicode utf-8 beautifulsoup urllib