Heim > Fragen und Antworten > Hauptteil
导入requests,提示报错 ''' UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 1: ordinal not in range(128)'''
问题查了,一般是字符之间转换的。但是到了库这里,就不懂了什么原因。。。。
小生愚钝,请教各位老师,请指点一二,麻烦了。
导入BeautifulSoup,它是正常的。下面是报错的具体信息。
PS :不是本专业的,想学习一点计算机方面的。学个爬虫玩玩。英语比较菜。。。
Traceback (most recent call last):
File "F:/untitled29/lianxi.py", line 5, in <module>
import requests
File "F:\Python27\lib\site-packages\requests\__init__.py", line 52, in <module>
from .packages.urllib3.contrib import pyopenssl
File "F:\Python27\lib\site-packages\requests\packages\urllib3\contrib\pyopenssl.py", line 47, in <module>
from cryptography import x509
File "F:\Python27\lib\site-packages\cryptography\x509\__init__.py", line 7, in <module>
from cryptography.x509.base import (
File "F:\Python27\lib\site-packages\cryptography\x509\base.py", line 16, in <module>
from cryptography.x509.extensions import Extension, ExtensionType
File "F:\Python27\lib\site-packages\cryptography\x509\extensions.py", line 14, in <module>
from asn1crypto.keys import PublicKeyInfo
File "F:\Python27\lib\site-packages\asn1crypto\keys.py", line 22, in <module>
from ._elliptic_curve import (
File "F:\Python27\lib\site-packages\asn1crypto\_elliptic_curve.py", line 51, in <module>
from ._int import inverse_mod
File "F:\Python27\lib\site-packages\asn1crypto\_int.py", line 56, in <module>
from ._perf._big_num_ctypes import libcrypto
File "F:\Python27\lib\site-packages\asn1crypto\_perf\_big_num_ctypes.py", line 31, in <module>
libcrypto_path = find_library('crypto')
File "F:\Python27\lib\ctypes\util.py", line 51, in find_library
fname = os.path.join(directory, name)
File "F:\Python27\lib\ntpath.py", line 85, in join
result_path = result_path + p_path
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 1: ordinal not in range(128)
阿神2017-04-18 10:36:16
看你的错误提示应该lanxi.py的编码格式有问题,你可以先到cmd控制台运行python再import试一下,应该不是requests有问题
高洛峰2017-04-18 10:36:16
result_path = result_path + p_path
这句代码的变量是不是有中文, 你可以都打印出来看看, 或者将它们都以unicode的形式存
result_path = u'xxx'
p_path = u'xxx'
或者 通过decode函数将变量转成unicode
巴扎黑2017-04-18 10:36:16
UnicodeDecodeError
是字符解码失败的原因,这不仅是requests
的问题,也不仅是python
的问题,所有编程语言都有这样的“问题”,也就是必须要了解字符编码。具体的字符编码可以查询资料。下面py2
中的字符编码问题。
py2
的用引号声明的字串类型都是str
,字串前加一个u
声明的才是unicode
。网络IO,文件读写中传输的字符都是编码成bytes,即str
类型。载入到计算机执行计算,一般都要解码成unicode
。py2的str
方法实际上是''.encode('ascii')
, unicode
方法是''.decode('ascii')
In [1]: s = u'你好'
In [2]: str(s)
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-2-d22ffcdd2ee9> in <module>()
----> 1 str(s)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
In [3]: s.decode('ascii')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-3-735804de5fd4> in <module>()
----> 1 s.decode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
In [4]: ss = '你好'
In [5]: unicode(ss)
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-5-6325006f91c2> in <module>()
----> 1 unicode(ss)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
In [6]: ss.decode('ascii')
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-6-b5dcf2f3b46d> in <module>()
----> 1 ss.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
In [7]: ss.decode('utf-8')
Out[7]: u'\u4f60\u597d'
In [8]: ss.decode('gbk')
Out[8]: u'\u6d63\u72b2\u30bd'
因为ss = '你好'
是非ascii
字符,因此以ascii
方式解码失败,当解码成utf-8
和gbk
就成功了。同理s=u'你好'
也不能编码成ascii
的方式。
你上面的问题,应该是非ascii
字符,decode
成ascii
字符的时候抛错。result_path + p_path
即这两个变量中,有一个变量是包含非ascii
字符的str
类型:
In [1]: 'hello' + u'world'
Out[1]: u'helloworld'
In [2]: 'hello' + u'世界'
Out[2]: u'hello\u4e16\u754c'
In [3]: '你好' + u'世界'
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-3-8c1827afc847> in <module>()
----> 1 '你好' + u'世界'
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
In [4]: '你好' + '世界'
Out[4]: '\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c'
In [5]: '你好' + '世界 world'
Out[5]: '\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c world'
In [6]: '你好' + u'世界 world'
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-6-dcdf837ec675> in <module>()
----> 1 '你好' + u'世界 world'
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
In [9]: '你好' + u'world'
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-9-1be7bc8e74d5> in <module>()
----> 1 '你好' + u'world'
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
'你好'
中的中文不是ascii
字符,和unicode字符拼接的时候,会解码成unicode再拼接,对于最后的例子,'你好' + u'world'
,其实执行的是 '你好'.decode('ascii') + u'world'
,所以就报错。
校正的方式很简单,统一字符编码就好。linux的py默认编码是utf-8,win貌似是gbk。不管怎么样,总之都用utf-8吧。
In [10]: '你好'.decode('utf-8') + u'world'
Out[10]: u'\u4f60\u597dworld'
py3中,所有引号声明的字串都是unicode。也就不存在str
和unicode
这两种类型。其中str
编码成bytes
类型,bytes
解码成字串类型。两种的相互转换的时候,还是会有 UnicodeDecodeError
问题,不要以为用了py3就能万事大吉,解决的问题关键是知道如何编码解码,就能一劳永逸。
>>> s = '中文'
>>> s.encode('utf-8')
b'\xe4\xb8\xad\xe6\x96\x87'
>>> s.encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> print(type(s.encode('utf-8')))
<class 'bytes'>
>>> print(type(s))
<class 'str'>