zoukankan      html  css  js  c++  java
  • 通过cookies自动登录readfree.me

    思路:

    1. 第一次手动录入验证码登录:
    2. 比对登录前后的cookies信息,获取关键信息登录的账号密码cookies;
    3. 添加登录的账号密码cookies登录网址;
    4. 可以设置为定时登录

    get_cookie.py

    # -*- coding: utf-8 -*-
    # @Time     : 2018/10/29 15:17
    # @Author   : Philly
    # @Site     : 
    # @File     : get_cookie.py
    # @Software : PyCharm Community Edition
    import yaml, time, os
    from selenium import webdriver
    
    url = 'http://readfree.me/'
    chromedriver_path = (r'C:Program Files (x86)GoogleChromeApplicationchromedriver.exe')
    driver = webdriver.Chrome(executable_path=chromedriver_path)
    driver.get(url)
    driver.maximize_window()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="navbar"]/div/div/ul/li[1]/a').click()
    time.sleep(2)
    cookie_before = driver.get_cookies()
    print('登录前的 cookies为:' + str(cookie_before))
    time.sleep(1)
    
    driver.find_element_by_id(u'id_email').clear()
    driver.find_element_by_id(u'id_email').send_keys('XXX') # 邮箱
    time.sleep(1)
    driver.find_element_by_id(u'id_password').clear()
    driver.find_element_by_id(u'id_password').send_keys('XXX')    # 密码
    print('请手动输入验证码:')
    security_code = input()
    time.sleep(3)
    driver.find_element_by_id('id_captcha_1').send_keys(security_code)
    time.sleep(3)
    driver.find_element_by_xpath('//*[@id="id_signin_form"]/div[1]/button').click()
    time.sleep(3)
    
    cookie_after = driver.get_cookies()
    print('得到的cookies为:' + str(cookie_after))
    len1 = len(cookie_after)
    cookie1 = cookie_after[0]   # 比对登录前后的cookie,获取账号密码的cookie
    fileNamePath = os.path.split(os.path.realpath(__file__))[0]
    yamlPath = os.path.join(fileNamePath, 'config.yaml')
    fw = open(yamlPath, 'w', encoding='utf-8')
    data = {"cookie1":cookie1}
    yaml.dump(data, fw)
    
    driver.quit()
    

    use_cookie.py

    # -*- coding: utf-8 -*-
    # @Time     : 2018/10/29 15:38
    # @Author   : Philly
    # @Site     : 
    # @File     : use_cookie.py
    # @Software : PyCharm Community Edition
    from selenium import webdriver
    import time, yaml, os
    
    url = 'http://readfree.me/'
    chromedriver_path = (r'C:Program Files (x86)GoogleChromeApplicationchromedriver.exe')
    driver = webdriver.Chrome(executable_path=chromedriver_path)
    driver.maximize_window()
    driver.delete_all_cookies()
    time.sleep(3)
    driver.get(url)
    
    fileNamePath = os.path.split(os.path.realpath(__file__))[0]
    yamlPath = os.path.join(fileNamePath, 'config.yaml')
    f = open(yamlPath, 'r', encoding='utf-8')
    cont = f.read()
    conf = yaml.load(cont)
    cookie1 = conf.get("cookie1")
    driver.add_cookie(cookie1)
    print("cookies: " + str(driver.get_cookies()))
    time.sleep(3)
    driver.get(url)
    time.sleep(2)
    driver.refresh()
    time.sleep(3)
    driver.quit()
    
    python D:SVNautocookieuser_cookie.py
    
  • 相关阅读:
    ASP.NET进阶(3):调用Javascript
    CMS系统模版引擎设计(3):Label基类的设计
    CMS系统模版引擎设计(1):基础类型
    CMS系统模板引擎设计(5):Label应用初探
    Thread系列——WaitHandle
    Thread系列——AutoResetEvent
    关于lock
    仅允许程序运行一个实例代码实现
    Thread系列——ManualResetEvent
    Thread系列——Thread.Join()
  • 原文地址:https://www.cnblogs.com/liuliu3/p/9871239.html
Copyright © 2011-2022 走看看