博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python发起请求提示UnicodeEncodeError
阅读量:7224 次
发布时间:2019-06-29

本文共 1498 字,大约阅读时间需要 4 分钟。

具体错误:

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

解决:

对请求参数进行编码处理:
示例代码:

import requestsimport jsonimport reimport pymysqlfrom tool.Mysql_connect import Mysql_operationfrom tool.get_token import Crm_tokenclass test_demo(object):    def __init__(self):        self.op_mysql=Mysql_operation()        self.token=Crm_token()    def create_yixiang(self):        url='http://xxx/customerAdjunctAdd'        token=self.token.get_token()        headers={
"Content-Type":'application/x-www-form-urlencoded', "token":token} try: tel_num=self.op_mysql.sql_select('''select max(tel) from nc_customer_adjunct''')[0]['max(tel)'] #结果为str except Exception as error: print(error) a=1 while a<3: tel_num=int(tel_num)+1 a+=1 data='customer_type=1&source=1&course_name_id=41&tel=%d&customer_name=测试3.1&sex=0&school=测试1&intro_id=0'%(tel_num) try: request1=requests.request("POST",url=url,headers=headers,data=data.encode()) #encode对请求编码处理:不处理接口会返回数据解析错误 # print(data) response1=request1.json() print(headers) print(response1) except Exception as error: print(error)if __name__=="__main__": Tm=test_demo() Tm.create_yixiang()

 

转载于:https://www.cnblogs.com/qtclm/p/10472474.html

你可能感兴趣的文章
我的友情链接
查看>>
TCP/IP协议及首部初了解
查看>>
防火墙iptables
查看>>
CUDA搭建
查看>>
memcached与PostgreSQL缓存命中机制
查看>>
百度地图路线检索(3)
查看>>
linux netstat 命令详解
查看>>
对前几篇blog的环境等的补充说明
查看>>
Curl命令使用解析大全
查看>>
MySQL日期函数
查看>>
【00】Effective Java
查看>>
.NET重构—单元测试重构
查看>>
SMB简介sabma服务(一)
查看>>
ANT简明教程
查看>>
Eclipse Luna WTP 与 Tomcat 8 的整合存在一个很头疼的 Bug
查看>>
小数在计算机里面的存放
查看>>
数据结构中的各种树简单解释
查看>>
我的朗科运维第七课
查看>>
CentOS的进程管理二
查看>>
https客户端证书导入
查看>>