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 ***


你可能感兴趣的:(python,Cookies,selenium,webdriver,requests)