#!/usr/bin/env python # -*- coding: utf-8 -*- import requests bot = requests.session() bot.get('http://google.com')
保持
import requests, pickle session = requests.session() # Make some calls
pickle.dump(session.get_cookies(), open('cookies.pkl', 'wb'))
加载
session = requests.session() # or an existing session
cookies = pickle.load(open('cookies.pkl', 'rb'))
for cookie in cookies:
session.cookies.update({'name': cookie['name'], 'value': cookie['value']})
cookie包含很多用不到的信息,比如path、secure等,在读取cookie只需要读取每个cookie的name和value属性