• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
oooooolr
You can do anything but not everything. ——David Allen
博客园    首页    新随笔    联系   管理    订阅  订阅

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
posted @ 2019-02-27 10:04  oooooolr  阅读(897)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3