首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >某些'utf-8‘编解码器无法解码字节

某些'utf-8‘编解码器无法解码字节
EN

Stack Overflow用户
提问于 2015-06-09 18:36:32
回答 2查看 2.1K关注 0票数 2

当我使用wget下载网站时,我得到一些错误

代码:

代码语言:javascript
复制
import threading
import urllib.request
import os
import re
import time
import json

def wget(url):
                #self.url = url 
    data = os.popen('wget -qO- %s'% url).read()
    return data

print (wget("http://jamesholm.se/dj.php"))

错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "stand-alone-check-url.py", line 13, in <module>
    print (wget("http://jamesholm.se/dj.php"))
  File "stand-alone-check-url.py", line 10, in wget
    data = os.popen('wget -qO- %s'% url).read()
  File "/usr/local/lib/python3.4/codecs.py", line 313, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9a in position 13133: invalid start byte

如何克服这个错误?

EN

回答 2

Stack Overflow用户

发布于 2015-06-09 19:54:29

您不能将任意字节序列解码为utf-8编码文本:

代码语言:javascript
复制
>>> b'\xa9'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 0: invalid start byte

该页面指示它使用utf-8,但服务器发送的实际数据不是utf-8。常有的事。

有一个bs4.UnicodeDammit可以让你处理编码不一致的数据:

代码语言:javascript
复制
import bs4 # $ pip install beautifulsoup4

print(bs4.UnicodeDammit.detwingle(b'S\x9aben  - Ostwind Rec').decode('utf-8'))
# -> Sšben  - Ostwind Rec
票数 1
EN

Stack Overflow用户

发布于 2015-06-12 03:43:12

使用requests python模块而不是wget。

代码语言:javascript
复制
>>> import requests
>>> data = requests.get("http://jamesholm.se/dj.php").text
>>> print(data)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30729633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档