Pythonブラウザcookiesをrequestsライブラリに共有


詳細はここを参照してください.次にコアコードを提供します.
#coding=utf-8
#@walker @2013-11-21
from selenium import webdriver
import time
import requests
b = webdriver.Firefox()
#b = webdriver.Ie()
b.delete_all_cookies()
cookies = {}
while True:
    list_cookies = b.get_cookies()  #             list
    print(list_cookies)
    for s in list_cookies:
        cookies[s['name']] = s['value']
    print(cookies)
    if cookies.has_key('BAIDUID'):
        b.close()
        break
    time.sleep(2)
          
session = requests.Session()
requests.utils.add_dict_to_cookiejar(session.cookies, cookies)

*** walker ***