编程语言
首页 > 编程语言> > python发起请求提示UnicodeEncodeError

python发起请求提示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.

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

import requests
import json
from urllib import parse

def create_yixiang():
a=0
url='http://XXXX/customerAdjunctAdd'
headers={"Content-Type":'application/x-www-form-urlencoded;charset=utf-8',

"token":'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.IntcImFkbWluX2lkXCI6OCxcImFkbWluX25hbWVcIjpcImFkbWluXCIsXCJ0ZWxcIjpcIjE4ODg4ODg4ODg4XCIsXCJyYW5kb21cIjpcIm1CRmM0U0tUTWsyaUNhV055Um9ZXCJ9Ig.gyRClVd_uJIWel8vA7Ubfs_Gigq8ADJC4izH9aYec5A'}
while a<2:
tel_num=18883612613+a
a+=1
data='customer_type=1&source=1&course_name_id=41&tel=%s&customer_name=测试3.1&sex=0&school=测试1&intro_id=0'%tel_num
# data='customer_type=1&source=%201&course_name_id=%2041&tel=%s&customer_name=%20%E6%B5%8B%E8%AF%953.1&sex=%200&school=%20%E6%B5%8B%E8%AF%951&intro_id=%200'%tel_num
# request1=requests.request("POST",url=url,headers=headers,data=data.encode("utf-8"))
request1=requests.request("POST",url=url,headers=headers,data=data.encode())
response1=request1.json()
print(response1)

if __name__=="__main__":
create_yixiang()

标签:__,tel,name,python,headers,UnicodeEncodeError,url,data,请求
来源: https://www.cnblogs.com/qtclm/p/10472474.html