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 判斷一組數(shù)據(jù)是否符合正態(tài)分布
這篇文章主要介紹了python 如何判斷一組數(shù)據(jù)是否符合正態(tài)分布,幫助大家更好的利用python分析數(shù)據(jù),感興趣的朋友可以了解下2020-09-09Python?OpenCV?Canny邊緣檢測算法的原理實現(xiàn)詳解
這篇文章主要介紹了Python?OpenCV?Canny邊緣檢測算法的原理實現(xiàn)詳解,由于邊緣檢測對噪聲敏感,因此對圖像應(yīng)用高斯平滑以幫助減少噪聲,具體詳情需要的小伙伴可以參考一下2022-07-07python實現(xiàn)class對象轉(zhuǎn)換成json/字典的方法
這篇文章主要介紹了python實現(xiàn)class對象轉(zhuǎn)換成json/字典的方法,結(jié)合實例形式分析了Python類型轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2016-03-03解決python web項目意外關(guān)閉,但占用端口的問題
今天小編就為大家分享一篇解決python web項目意外關(guān)閉,但占用端口的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12使用python搭建代理IP池實現(xiàn)接口設(shè)置與整體調(diào)度
在網(wǎng)絡(luò)爬蟲中,代理IP池是一個非常重要的組件,由于許多網(wǎng)站對單個IP的請求有限制,因此,我們需要一個代理IP池,在本文中,我們將使用Python來構(gòu)建一個代理IP池,然后,我們將使用這個代理IP池來訪問我們需要的數(shù)據(jù),文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2023-12-12Python 中類的構(gòu)造方法 __New__的妙用
這篇文章主要介紹了Python 中類的構(gòu)造方法 New的妙用,Python 的類中,所有以雙下劃線__包起來的方法,叫魔術(shù)方法,魔術(shù)方法在類或?qū)ο蟮哪承┦录l(fā)出后可以自動執(zhí)行,讓類具有神奇的魔力。下面就來學(xué)習(xí)文章的詳細內(nèi)容把2021-10-10