• requests 遇到报错UnicodeEncodeError: 'latin-1' codec can't encode character 'u86c7' in position 8: Body ('蛇') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.


    错误描述

    Traceback (most recent call last):
      File "C:/Users/zuimojun/PycharmProjects/pythonProject4/requestdata/readexcl.py", line 23, in <module>
        print(rc.requestsExcelMethod())
      File "C:/Users/zuimojun/PycharmProjects/pythonProject4/requestdata/readexcl.py", line 17, in requestsExcelMethod
        rr = requests.post(excel_i["url"], data=excel_i["params"])
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libsite-packages
    equestsapi.py", line 116, in post
        return request('post', url, data=data, json=json, **kwargs)
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libsite-packages
    equestsapi.py", line 60, in request
        return session.request(method=method, url=url, **kwargs)
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libsite-packages
    equestssessions.py", line 533, in request
        resp = self.send(prep, **send_kwargs)
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libsite-packages
    equestssessions.py", line 646, in send
        r = adapter.send(request, **kwargs)
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libsite-packages
    equestsadapters.py", line 449, in send
        timeout=timeout
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libsite-packagesurllib3connectionpool.py", line 672, in urlopen
        chunked=chunked,
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libsite-packagesurllib3connectionpool.py", line 387, in _make_request
        conn.request(method, url, **httplib_request_kw)
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libhttpclient.py", line 1229, in request
        self._send_request(method, url, body, headers, encode_chunked)
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libhttpclient.py", line 1274, in _send_request
        body = _encode(body, 'body')
      File "C:UserszuimojunAppDataLocalProgramsPythonPython37libhttpclient.py", line 160, in _encode
        (name.title(), data[err.start:err.end], name)) from None
    UnicodeEncodeError: 'latin-1' codec can't encode character 'u86c7' in position 8: Body ('蛇') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
    
    
    
    
    
    
    #### 解决方法:
      .encode("utf-8").decode("latin1")        在发生编码错误的地方使用这个即可    
    我的是在params参数时发证编码错误的,所以要在params或者data值后面添加。
  • 相关阅读:
    flask——flask-sqlachemy中的一对多,多对多关系
    flask——flask-sqlachemy的使用,模块划分
    Ubuntu下安装JDK和Open Jdk
    flask——全文检索
    Flask——部署
    在Ubuntu上安装Chrome浏览器和ChromeDriver
    jinja2模板语言的循环序号
    CentOS7安装mongodb
    CentOS7安装PhantomJS
    使用screen在后台运行程序
  • 原文地址:https://www.cnblogs.com/zjldeboke/p/14860668.html
Copyright © 2020-2023  润新知