1.requests里设置cookies,可以将cookies放入headers里一同提交。
{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': '__cfduid=d50cf692a2f654986d51b98e6c1bad6c41535939301; session=.eJwlzj0OwjAMQOG7eO6Q2Emc9DKV6x-oEB1amBB3pxLS27_3gSUOP-8wv463T7BsBjMEiaIRMZe-MgfXKozmShqWXZVMOqdW-jDElkbm2r2IKqt0WYkwczKLNjyKmqUW2Nekgv3KMg2mWp1TRcm54ShNROvl5FARmOB9-vGfeWz7bZenw_cHGzsyUA.Dw5FeA.xD0ofpGXJUvDLYeCrMn08E24HKM',
'DNT': '',
'Host': 'exercise.kingname.info',
'Upgrade-Insecure-Requests': '',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
2.但是,如果使用Scrapy的时候需要Cookies,就不能把Cookies放在Headers里面。在Scrapy发起请求的时候,有一个单独的参数来设置Cookies:
yield scrapy.Request(url,
callback=self.xxx,
headers=headers,
cookies={'__cfduid': 'd50cf692a2f654986d51b98e6c1bad6c41535939301',
'session': '.eJwlzj0OwjAMQOG7eO6Q2Emc9DKV6x-oEB1amBB3pxLS27_3gSUOP-8wv463T7BsBjMEiaIRMZe-MgfXKozmShqWXZVMOqdW-jDElkbm2r2IKqt0WYkwczKLNjyKmqUW2Nekgv3KMg2mWp1TRcm54ShNROvl5FARmOB9-vGfeWz7bZenw_cHGzsyUA.Dw5FeA.xD0ofpGXJUvDLYeCrMn08E24HKM'})