导入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)
If you just want to play around, you can consider using Python3 directly. Compared with Python2, Python3 will have much fewer character encoding problems.
Looking at your error message, there should be a problem with the encoding format of lanxi.py. You can first go to the cmd console and run python and then import to try. It should not be a problem with requests
The folder path of the ssl encryption function package has special characters
Change to py3, 2 always has various coding problems
If the variables in this code have Chinese characters, you can print them out to see, or save them all in unicode form
Character encoding issues in
UnicodeDecodeError
是字符解码失败的原因,这不仅是requests
的问题,也不仅是python
的问题,所有编程语言都有这样的“问题”,也就是必须要了解字符编码。具体的字符编码可以查询资料。下面py2
.py2
的用引号声明的字串类型都是str
,字串前加一个u
声明的才是unicode
。网络IO,文件读写中传输的字符都是编码成bytes,即str
类型。载入到计算机执行计算,一般都要解码成unicode
。py2的str
方法实际上是''.encode('ascii')
,unicode
方法是''.decode('ascii')
Because of the way
ss = '你好'
是非ascii
字符,因此以ascii
方式解码失败,当解码成utf-8
和gbk
就成功了。同理s=u'你好'
也不能编码成ascii
.Your question above should be non-
ascii
字符,decode
成ascii
字符的时候抛错。result_path + p_path
即这两个变量中,有一个变量是包含非ascii
字符的str
Type:'你好'
中的中文不是ascii
字符,和unicode字符拼接的时候,会解码成unicode再拼接,对于最后的例子,'你好' + u'world'
,其实执行的是'你好'.decode('ascii') + u'world'
, so I reported an error.The correction method is very simple, just use unified character encoding. The default encoding of py in Linux is utf-8, and it seems to be gbk in win. No matter what, use utf-8 anyway.
In py3, all strings declared in quotation marks are unicode. There is no
str
和unicode
这两种类型。其中str
编码成bytes
类型,bytes
解码成字串类型。两种的相互转换的时候,还是会有UnicodeDecodeError
problem. Don’t think that everything will be fine by using py3. The key to solving the problem is to know how to encode and decode, and you can solve it once and for all.