Python常见问题 - requests请求参数包含中文报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: 小明 is not valid Latin-1. Use body.encode('utf-8')

简介: Python常见问题 - requests请求参数包含中文报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: 小明 is not valid Latin-1. Use body.encode('utf-8')

背景


在做接口自动化的时候,Excel作为数据驱动,里面存了中文,通过第三方库读取中文当请求参数传入 requests.post() 里面,就会报错

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: Body ('小明') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.



此时我封装的post请求方法是这样的

def post(self, url, data=None, file=None, header=None, cookie=None):
    try:
        res = requests.post(url, data=data, files=file, headers=header, cookies=cookie, verify=False)
        return res
    except BaseException as e:
        loggings.error("post请求错误,错误原因:%s" % e)
        raise e


这个时候,我们要将传入的参数按照utf-8格式编码

# 先转成Json字符串
data = json.dumps(data) 
# 按照utf-8编码成字节码 
data = data.encode("utf-8")


改造后的方法如下


def post(self, url, data=None,  file=None, header=None, cookie=None):
    try:
        data = json.dumps(data) 
        data = data.encode("utf-8")
        res = requests.post(url, data=data, files=file, headers=header, cookies=cookie, verify=False)
        return res
    except BaseException as e:
        loggings.error("post请求错误,错误原因:%s" % e)
        raise e


当然有更简单的方法就是将字典数据转成json字符串传入,请求参数名叫 data ,代码如下:

def post(self, url, data=None,  file=None, header=None, cookie=None):
    try:
        data = json.dumps(data) 
        res = requests.post(url, json=data, files=file, headers=header, cookies=cookie, verify=False)
        return res
    except BaseException as e:
        loggings.error("post请求错误,错误原因:%s" % e)
        raise e



相关文章
|
10月前
|
异构计算 Python
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
774 1
|
10月前
|
人工智能 Shell Python
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
366 0
|
12月前
|
数据采集 机器学习/深度学习 边缘计算
Python爬虫动态IP代理报错全解析:从问题定位到实战优化
本文详解爬虫代理设置常见报错场景及解决方案,涵盖IP失效、403封禁、性能瓶颈等问题,提供动态IP代理的12种核心处理方案及完整代码实现,助力提升爬虫系统稳定性。
600 0
|
Python
解决Python报错:DataFrame对象没有concat属性的多种方法(解决方案汇总)
总的来说,解决“DataFrame对象没有concat属性”的错误的关键是理解concat函数应该如何正确使用,以及Pandas库提供了哪些其他的数据连接方法。希望这些方法能帮助你解决问题。记住,编程就像是解谜游戏,每一个错误都是一个谜题,解决它们需要耐心和细心。
646 15
|
人工智能 Shell 开发工具
[oeasy]python065python报错怎么办_try_试着来_except_发现异常
本文介绍了Python中处理异常的基本方法,重点讲解了`try`和`except`的用法。通过一个计算苹果重量的小程序示例,展示了如何捕获用户输入错误并进行处理。主要内容包括: 1. **回顾上次内容**:简要回顾了Shell环境、Python3游乐场和Vim编辑器的使用。 2. **编写程序**:编写了一个简单的程序来计算苹果的总重量,但发现由于输入类型问题导致结果错误。 3. **调试与修正**:通过调试发现输入函数返回的是字符串类型,需要将其转换为整数类型才能正确计算。
472 32
|
Linux Python
【Azure Function】Python Function部署到Azure后报错No module named '_cffi_backend'
ERROR: Error: No module named '_cffi_backend', Cannot find module. Please check the requirements.txt file for the missing module.
472 2
|
Python
Python的报错让我学到新知识
Python的报错让我学到新知识
607 0
|
数据采集 网络安全 Python
Python使用urllib或者urllib2模块打开网页遇到ssl报错
Python使用urllib或者urllib2模块打开网页遇到ssl报错
1016 0
|
缓存 Python
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-npf9报错
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-npf9报错
|
JSON 安全 数据格式
7-6|python报错TypeError: can't pickle _thread.RLock objects
7-6|python报错TypeError: can't pickle _thread.RLock objects

推荐镜像

更多