session请求示例
1. requests session
requests
自带session
管理, 示例:
import json
import requests
with requests.Session() as session:
session.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
r = session.get('https://httpbin.org/cookies')
assert r.status_code == 200
assert json.loads(r.text)["cookies"]["sessioncookie"] == "123456789"
2. scrapy session
scrapy
使用cookiejar
管理session
. 参考.
def start_first_page(self, ):
yield scrapy.Request("https://httpbin.org/cookies/set/sessioncookie/123456789", meta={'cookiejar': 0},
callback=self.parse_second_page)
def parse_second_page(self, response):
return scrapy.Request("https://httpbin.org/cookies",
meta={'cookiejar': response.meta['cookiejar']},
callback=self.parse_other_page)
3. tornado client + session
tornado
本身不带session
模块, 客户端可使用cookies
维护session.
获取新cookies
:
cookies = response.headers.get_list('Set-Cookie')
使用新cookies
:
import tornado.httpclient
http_client = tornado.httpclient.HTTPClient()
# cookies = {"Cookie" : 'my_cookie=abc'}
http_client.fetch("http://abc.com/test", headers=cookies)