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

yipeiwu_com6年前Python基础

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参数不能有中文的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用matlab或python将txt文件转为excel表格

使用matlab或python将txt文件转为excel表格

假设txt文件为: 一、matlab代码 data=importdata('data.txt'); xlswrite('data.xls',data); 二、python代码...

Python计时相关操作详解【time,datetime】

本文实例讲述了Python计时相关操作。分享给大家供大家参考,具体如下: 内容目录: 1. 时间戳 2. 当前时间 3. 时间差 4. python中时间日期格式化符号 5. 例子 一、...

使用python动态生成波形曲线的实现

使用python动态生成波形曲线的实现

效果是这个样子的: 用到的模块: * matplotlib.pyplot * matplotlib.animation.FuncAnimation * numpy 三个圆的半径分...

详解Python self 参数

1、概述 1.1 场景 我们在使用 Python 中的 方法 method 时,经常会看到 参数中带有 self,但是我们也没对这个参数进行赋值,那么这个参数到底是啥意思呢? 2、知识点...

详解Python中__str__和__repr__方法的区别

 对我当前工程进行全部测试需要花费不少时间。既然有 26 GB 空闲内存,为何不让其发挥余热呢? tmpfs 可以通过把文件系统保存在大内存中来加速测试的执行效率。 但...