scrapy:get cookie from response

scrapy shell
fetch('your_url')
response.headers.getlist("Set-Cookie")
https://stackoverflow.com/questions/46543143/scrapy-get-cookies-from-response-request-headers
 response.headers 返回所有的headers
 response.headers.get("Set-Cookie") 返回的是byte类型
b'token=P85TRJJ1C7; expires=Wed, 27-Feb-2019 01:54:18 GMT; Max-Age=30; Path=/'
 response.headers.getlist("set-cookie")

[b'__cfduid=d725f0c6f730503571750709bcce5339e1551232427; expires=Thu, 27-Feb-20 01:53:47 GMT; path=/; domain=.scrapingclub.com; HttpOnly',

b'token=P85TRJJ1C7; expires=Wed, 27-Feb-2019 01:54:18 GMT; Max-Age=30; Path=/']

b'表示是byte,
response.headers.get("Set-Cookie").decode("utf-8")  可以encode成sentence
pa=re.compile('token=(.*?);')

pa.findall( response.headers.get("set-cookie").decode("utf-8")) 返回的是list列表,用[0]get内容
headers 没有带x-requested-with:XMLHttpRequest 
请求不成功,带上之后请求成功
可以用来判断客户端的请求是Ajax请求还是其他请求。。
若 req.headers['x-requested-with'].toLowerCase() == 'xmlhttprequest' 则为ajax请求。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- focus on what you want to be
原文地址:https://www.cnblogs.com/bamboozone/p/10441769.html