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

yipeiwu_com5年前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设计】。

相关文章

Python_查看sqlite3表结构,查询语句的示例代码

如下所示: #!/usr/bin/env python3 # -*- coding: utf-8 -*- import sqlite3 conn = sqlite3.conn...

浅谈python中的占位符

占位符,顾名思义就是插在输出里站位的符号。我们可以把它理解成我们预定饭店。当我们告诉饭店的时候,饭店的系统里会有我们的预定位置。虽然我们现在没有去但是后来的顾客就排在我们后面。 常见的占...

python时间日期函数与利用pandas进行时间序列处理详解

python标准库包含于日期(date)和时间(time)数据的数据类型,datetime、time以及calendar模块会被经常用到。 datetime以毫秒形式存储日期和时间,da...

Python for循环与range函数的使用详解

for 循环 For … in 语句是另一种循环语句,其特点是会在一系列对象上进行迭代(Iterates),即它会遍历序列中的每一个项目 注意: 1、else 部分是可选的。当循环中包含...

对pandas replace函数的使用方法小结

对pandas replace函数的使用方法小结

语法:replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method=...