我提取到了一个cookie,是字典的形式,我将它转换成了字符串去修改其中的内容,最后需要将它再转换成字典添加到我的请求头中,但是使用eval函数后出现了空格,这样再添加到请求头中就会报错,有什么办法能消除这个空格吗
代码如下
import requests
import json
url1 = "http://yktwx.henau.edu.cn/wechat/login/wechat_login.html"
url="http://yktwx.henau.edu.cn/wechat/url/redirectJkbh.html?jkbh=0006"
hd = {'Host':'yktwx.henau.edu.cn',
'Connection':'keep-alive',
'Upgrade-Insecure-Requests':'1',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9',
'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.10(0x18000a29) NetType/WIFI Language/zh_CN',
'Accept-Language':'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'Accept-Encoding':'gzip, deflate',
'Connection':'keep-alive'}
session = requests.session()
response = session.get("
http://yktwx.henau.edu.cn/wechat/url/redirectJkbh.html?jkbh=0006%22,headers=hd
)
html_set_cookie = requests.utils.dict_from_cookiejar(session.cookies)
print(html_set_cookie)
'''hd2 = hd.update(html_set_cookie)
print(hd2)'''
#先转化为字符串,然后用replace
c=str(html_set_cookie).replace("JSESSIONID': '","Cookie':'JSESSIONID=")
d = eval(c)
print(c)
print(d)