selenium获取cookies

自动化测试工具selenium,利用浏览器的驱动,可以有效的模拟人类对浏览器的操作,也可以随时获得最新的cookies

#大概格式如下,具体内容请自行按需修改
def get_cookie_from_network():
    from selenium import webdriver
    url_login = 'http://login.weibo.cn/login/' 
    driver = webdriver.PhantomJS()
    driver.get(url_login)
    driver.find_element_by_xpath('//input[@type="text"]').send_keys('your_weibo_accout') # 改成你的微博账号
    driver.find_element_by_xpath('//input[@type="password"]').send_keys('your_weibo_password') # 改成你的微博密码

    driver.find_element_by_xpath('//input[@type="submit"]').click() # 点击登录

    # 获得 cookie信息
    cookie_list = driver.get_cookies()
    print cookie_list

    cookie_dict = {}
    for cookie in cookie_list:
        #写入文件
        f = open(cookie['name']+'.weibo','w')
        pickle.dump(cookie, f)
        f.close()

        if cookie.has_key('name') and cookie.has_key('value'):
            cookie_dict[cookie['name']] = cookie['value']

    return cookie_dict

猜你喜欢

转载自blog.csdn.net/qq_38059635/article/details/81458003