解决python3 requests headers参数不能有中文的问题

纯洁美丽。它化做雪花,用自己洁白的生躯覆盖在大地上。此时,一切都是银装素裹,一副冰清玉洁的模样。光秃秃的树枝上缀满了朵朵洁白的 "梨花 ",风一吹,花儿纷纷飘落,好似一只只银白色的蝴蝶在空中偏偏起舞。 "忽如一夜春风来,千树万树梨花开 ",这句诗不正是赞美冬天的吗?

1 需求,heeaders 参数需要拼接中文参数param 解决如下

url = 'https://....search?keyword=' + param + '&templateId=&page=1&pageSize=10'
headers = {
 "Accept": "application/json, text/javascript, */*; q=0.01",
 "Accept-Encoding": "gzip, deflate, br",
 "Accept-Language": "zh-CN,zh;q=0.9",
 "Connection": "keep-alive",
 "Cookie": "Hm_lvt_0076fef7e919d8d7b24383dc8f1c852a=1522398473; Hm_lpvt_0076fef7e919d8d7b24383dc8f1c852a=1522398482",
 "Referer": "https://...../index.html?index=0&keyword=" + urllib.parse.quote(param),
 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
 "X-Requested-With": "XMLHttpRequest"
}
rsp = requests.get(url, headers=header 
from urllib import parse 
 
str1 = 'haha哈哈' 
str2 = parse.quote(str1)  #quote()将字符串进行编码 
print(str2)        #str2=haha%E5%93%88%E5%93%88 
str3 = parse.unquote(str2) #解码字符串 
print(str3)        #str3=haha哈哈 

以上这篇解决python3 requests headers参数不能有中文的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签: 能有 requests