def use_cookie(self):
cookies="YF-V5-G0=731b77772529a1f49eac82a9d2c2957f; SUB=_2AkMsEgief8NxqwJRmPASyGvkbYl3wwzEieKaTvlFJRMxHRl-yT83ql0ltRB6B5ImcWJgp-T_qKP4KYQRB6SAM7ufKcM0; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9Whzk8QS7FTR.ymDWBYYns0L"
cookies2 = dict(map(lambda x:x.split('='),cookies.split(";")))
print(cookies2)
#1.requests的headers方式
#self.headers.setdefault("Cookie",cookies)
#req=requests.get(self.url,headers=self.headers,timeout=60) #方式1加到headers
#req = requests.get(self.url,headers=self.headers,cookies=cookies2,timeout=60)#方式2加到cookie字段,值类型可以为字典
# 方式3定义生成session对象然后通过session.cookies.set设置
# for k,v in cookies2.items():
# self.session.cookies.set(k,v)
self.session.headers.update({
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36",
})
# 方式3定义生成session对象然后通过session的cookies属性,直接赋值
for k,v in cookies2.items():
self.session.cookies[k]=v
req=self.session.get(self.url)
if req.status_code==200:
req.encoding=req.apparent_encoding
print(self.session.cookies.get_dict())#获取cookies
print(req.json().get('data').get('html'))