2014-09-03 53 views
0

我正面临着美丽的一些问题。Python 3&BeautifulSoup问题 - [解码错误 - 输出不是utf-8]

我想看过一些网站的标题,当我的代码试图读取一些网站,包含与拉美本地字符的头衔,我得到这个错误:

[Decode error - output not utf-8]

是否有人知道如何解决这个问题?

干杯。

我的代码:

def getTitle(theList): 

for element in theList: 

    response = urllib.request.urlopen(element) 
    soup = BeautifulSoup(response.read()) 
    title = soup.find("title").text 
    print (element,": ",title,"\n") 

回答

0

如果错误是由于inconsitent encodings即HTML大多是UTF-8(以及BeautifulSoup检测为这样),但一些字符(最显着的是:微软智能报价)使用不同的编码;尝试使用UnicodeDammit.detwingle()方法使文档成为纯UTF-8而不丢弃可挽救的不正确字符。

+0

塞巴斯蒂安,我不明白。你可以再详细一点吗?我应该在BeautifulSoup(...)中使用UnicodeDammint.getwingle()吗? – 2014-09-03 23:40:50

+0

@ user3120065:点击链接。它明确地说什么时候你应该调用'.detwingle()' – jfs 2014-09-03 23:44:21