python 的requests问题
迷茫
迷茫 2017-04-18 10:34:16
0
5
1009

导入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)
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(5)
Ty80

如果只是玩一玩的話可以考慮直接使用Python3,比起Python2,Python3的字元編碼問題會少很多。

阿神

看你的錯誤提示應該lanxi.py的編碼格式有問題,你可以先到cmd控制台運行python再import試一下,應該不是requests有問題

大家讲道理

ssl 的加密功能包的資料夾路徑有特殊字元

換py3吧, 2總是有各種各樣的編碼問題

小葫芦
result_path = result_path + p_path 

這句程式碼的變數是不是有中文, 你可以都印出來看看, 或是將它們都以unicode的形式存

result_path = u'xxx'
p_path = u'xxx'
或者 通过decode函数将变量转成unicode
巴扎黑

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-8gbk就成功了。同理s=u'你好'也不能编码成ascii的方式。

你上面的問題,應該是非ascii字符,decodeascii字符的时候抛错。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。也就不存在strunicode这两种类型。其中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'>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板