良许Linux教程网 干货合集 Python UnicodeEncodeError 问题解决方法

Python UnicodeEncodeError 问题解决方法

本篇文章重点为大家讲解一下 Python UnicodeEncodeError 问题,有需要的小伙伴可以参考一下。

image-20220216232019969

具体错误:

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 requests
import json
import re
import pymysql
from tool.Mysql_connect import Mysql_operation
from tool.get_token import Crm_token

class 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'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()

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部