Python發(fā)起請求提示UnicodeEncodeError錯誤代碼解決方法
具體錯誤:
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.
解決:
對請求參數(shù)進行編碼處理:
示例代碼:
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)'] #結(jié)果為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對請求編碼處理:不處理接口會返回數(shù)據(jù)解析錯誤
# print(data)
response1=request1.json()
print(headers)
print(response1)
except Exception as error:
print(error)
if __name__=="__main__":
Tm=test_demo()
Tm.create_yixiang()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python接口自動化淺析logging日志原理及模塊操作流程
這篇文章主要為大家介紹了Python接口自動化系列文章淺析logging日志原理及模塊操作流程,文中詳細(xì)說明了為什么需要日志?日志是什么?以及日志用途等基本的原理2021-08-08
Python Switch Case三種實現(xiàn)方法代碼實例
這篇文章主要介紹了Python Switch Case2種實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python使用time模塊實現(xiàn)指定時間觸發(fā)器示例
這篇文章主要介紹了Python使用time模塊實現(xiàn)指定時間觸發(fā)器,結(jié)合實例形式分析了Python時間相關(guān)模塊與方法使用技巧,需要的朋友可以參考下2017-05-05
Python設(shè)計模式中的創(chuàng)建型工廠模式
這篇文章主要介紹了Python設(shè)計模式中的創(chuàng)建型工廠模式,工廠模式即Factory?Pattern,是提供創(chuàng)建對象的最佳方式,下文小編介紹Python工廠模式的相關(guān)資料,需要的朋友可以參考一下2022-02-02
python3 圖片 4通道轉(zhuǎn)成3通道 1通道轉(zhuǎn)成3通道 圖片壓縮實例
今天小編就為大家分享一篇python3 圖片 4通道轉(zhuǎn)成3通道 1通道轉(zhuǎn)成3通道 圖片壓縮實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

