python2 requests报错:“UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd3 in position 252”

报错代码:
url中有中文,并且编码格式是gbk,然后报错

r = requests.get(url)
print(url)  # 'http://192.168.0.200:8019/staf/\xb4\xf2\xb0\xfc/muban.py'

报错详情

Traceback (most recent call last):
  File "", line 1, in <module>
  File "C:\python\py27_staf\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\python\py27_staf\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\python\py27_staf\lib\site-packages\requests\sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "C:\python\py27_staf\lib\site-packages\requests\sessions.py", line 453, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\python\py27_staf\lib\site-packages\requests\models.py", line 318, in prepare
    self.prepare_url(url, params)
  File "C:\python\py27_staf\lib\site-packages\requests\models.py", line 368, in prepare_url
    url = url.decode('utf8')
  File "C:\python\py27_staf\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb4 in position 31: invalid start byte

解决方案

r = requests.get(url.decode('gbk'))

你可能感兴趣的:(python,开发语言)