python 3中的requests.get和deencodeerror?
我在Python 3中的请求库方面遇到了问题 这是我的代码:python 3中的requests.get和deencodeerror?,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,我在Python 3中的请求库方面遇到了问题 这是我的代码: url = 'https://www.contrataciones.gov.py/images/opendata/planificaciones/2016.csv' r = requests.get(url) reader = csv.DictReader(r.content.splitlines()) 当我使用Python 2运行脚本时,它工作得非常好,但是使用Python 3时,我在请求中失败了。get(url)行: Unico
url = 'https://www.contrataciones.gov.py/images/opendata/planificaciones/2016.csv'
r = requests.get(url)
reader = csv.DictReader(r.content.splitlines())
当我使用Python 2运行脚本时,它工作得非常好,但是使用Python 3时,我在请求中失败了。get(url)
行:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 359: ordinal not in range(128)
我做错了什么?我知道如何解码内容等等,但直接从请求中获取错误会让我感到困惑
更新:完全回溯-看起来可能与pickle有关
File "fetch.py", line 128, in <module>
main()
File "fetch.py", line 115, in main
id_list = fetchList(options.year)
File "/usr/local/lib/python3.6/site-packages/ratelimit/__init__.py", line 21, in func_wrapper
ret = func(*args, **kargs)
File "fetch.py", line 89, in fetchList
r = requests.get(url)
File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.6/site-packages/requests_cache/core.py", line 126, in request
**kwargs
File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.6/site-packages/requests_cache/core.py", line 97, in send
response, timestamp = self.cache.get_response_and_time(cache_key)
File "/usr/local/lib/python3.6/site-packages/requests_cache/backends/base.py", line 70, in get_response_and_time
if key not in self.responses:
File "/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_collections_abc.py", line 666, in __contains__
self[key]
File "/usr/local/lib/python3.6/site-packages/requests_cache/backends/storage/dbdict.py", line 163, in __getitem__
return pickle.loads(bytes(super(DbPickleDict, self).__getitem__(key)))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 359: ordinal not in range(128)
文件“fetch.py”,第128行,在
main()
文件“fetch.py”,第115行,在main中
id\u list=fetchList(options.year)
文件“/usr/local/lib/python3.6/site packages/ratelimit/_init__.py”,第21行,在func_包装中
ret=func(*args,**kargs)
文件“fetch.py”,第89行,在fetchList中
r=请求。获取(url)
文件“/usr/local/lib/python3.6/site packages/requests/api.py”,第70行,在get中
返回请求('get',url,params=params,**kwargs)
文件“/usr/local/lib/python3.6/site packages/requests/api.py”,请求中的第56行
return session.request(method=method,url=url,**kwargs)
文件“/usr/local/lib/python3.6/site packages/requests\u cache/core.py”,请求中的第126行
**夸尔斯
文件“/usr/local/lib/python3.6/site packages/requests/sessions.py”,请求中第488行
resp=自我发送(准备,**发送)
文件“/usr/local/lib/python3.6/site packages/requests\u cache/core.py”,第97行,在send中
响应,timestamp=self.cache.get\u响应和时间(cache\u键)
文件“/usr/local/lib/python3.6/site packages/requests\u cache/backends/base.py”,第70行,在get\u response\u和\u time中
如果未输入self.responses:
文件“/usr/local/cillar/python3/3.6.0\u 1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/\u collections\u abc.py”,第666行,位于__
自动[钥匙]
文件“/usr/local/lib/python3.6/site packages/requests\u cache/backends/storage/dbdict.py”,第163行,在u getitem中__
返回pickle.load(字节(super(DbPickleDict,self)。\uuu getitem\uuuu(key)))
UnicodeDecodeError:“ascii”编解码器无法解码位置359处的字节0xc3:序号不在范围内(128)
在请求之前,您必须解码您的url
试试这个:
newurl = url.decode("utf-8")
r = requests.get(newurl)
在Python 2中,隐式str类型是ASCII。但在Python3.x中,隐式str类型是Unicode
print(type('default string '))
print(type(b'string with b '))
'''
Output in Python 2.x (Bytes is same as str)
<type 'str'>
<type 'str'>
Output in Python 3.x (Bytes and str are different)
<class 'str'>
<class 'bytes'>
'''
这里有解释,解码函数只对编码字符串有效
>>> s = "x,jnvjlf"
>>> d = s.encode("utf-8")
>>> d
b'x,jnvjlf'
>>> e = s.decode("utf-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>> e = d.decode("utf-8")
>>> e
'x,jnvjlf'
>>s=“x,jnvjlf”
>>>d=s.encode(“utf-8”)
>>>d
b'x,jnvjlf'
>>>e=s.decode(“utf-8”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“str”对象没有属性“decode”
>>>e=d.解码(“utf-8”)
>>>e
“x,jnvjlf”
无法复制。顺便说一句,Python2.x内置的csv
模块不支持unicode,如果csv是utf-8编码的,可以作为替代品使用。嗯-我想知道是不是因为我先在Python2中运行了它,然后在Python3中运行了它,pickle弄糊涂了?这是一个非常愚蠢但致命的错误,很抱歉,字符串“url”应该被解码。道歉!我只得到了AttributeError:“str”对象没有属性“decode”
。为什么这会造成不同?我必须将importlib import reload中的添加到目标Python 3中,然后我得到AttributeError:module'sys'没有属性“setdefaultencoding”
。如果您使用pickle,请尝试像这样加载您的数据pickle.load(file,encoding='latin1')
,如果可能的话,再显示一些代码
>>> s = "x,jnvjlf"
>>> d = s.encode("utf-8")
>>> d
b'x,jnvjlf'
>>> e = s.decode("utf-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>> e = d.decode("utf-8")
>>> e
'x,jnvjlf'