Selenium cookies for requests

2020-01-02 18:04:34 浏览数 (1)

使用selenium模拟登录,保存cookies

代码示例:

代码语言:javascript复制
import json
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

def selenium_login_apple(username=None, password=None):
    if not username:
        username = 'your username'
    if not password:
        password = 'your password'
   
    url = "URL"
    option = webdriver.ChromeOptions()
    option.add_argument('--headless')
    option.add_argument('--no-sandbox')
    option.add_argument('--disable-gpu')
    option.add_argument('--disable-infobars')
    option.add_argument('--start-maximized')
    # option.add_argument('--hide-scrollbars')
    # option.add_argument('blink-settings=imagesEnabled=false')
    # 开发者模式打开,防止被检测
    # option.add_argument("--incognito")
    # option.add_argument("--disable-plugins-discovery")
    # option.add_argument('--profile-directory=Default')
    # option.add_argument('--disable-extensions')
    option.add_argument(
        'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36')
    
    browser = webdriver.Chrome(chrome_options=option)
    browser.delete_all_cookies()  # 清除cookies
    try:
        browser.get(url)
        # 隐式等待
        browser.implicitly_wait(60)
        # 切换到iframe里面,如果没有不需要,下面每一步都等待零点几秒是防止上一步还没完成,就执行了下一步
        browser.switch_to.frame(browser.find_element_by_xpath("//iframe"))
        # 输入用户名
        browser.find_element_by_xpath("//input[@id='account_name_text_field']").send_keys(username)
        time.sleep(0.2)
        # Keys.ENTER 输入回车键
        browser.find_element_by_xpath("//input[@id='account_name_text_field']").send_keys(Keys.ENTER)
        time.sleep(0.3)
        # 输入密码
        browser.find_element_by_xpath("//input[@id='password_text_field']").send_keys(password)
        time.sleep(0.2)
        browser.find_element_by_xpath("//input[@id='password_text_field']").send_keys(Keys.ENTER)
        time.sleep(0.3)
        # # 等待跳转到提问页面
        browser.implicitly_wait(60)
        time.sleep(3)
        # 获取cookies
        login_rear_cookie = browser.get_cookies()
        tmp_dict = {}
        if login_rear_cookie:
            for item in login_rear_cookie:
                tmp_dict.setdefault(item["name"], item["value"])
            # 把cookie信息用json序列化后写入cookie.txt文件
            print(tmp_dict)
            with open('cookie.txt', 'w') as f:
                f.write(json.dumps(login_rear_cookie))
    except Exception as e:
        print(e)
    finally:
        browser.quit()

执行上面的代码会生成一个cookie.txt文件,下面重点来了,requests怎么使用selenium的cookie

示例代码:

代码语言:javascript复制
import requests
import json

# file_path为cookie.txt的路径
def search_apple_id(file_path):
    s = requests.session()
    s.keep_alive = False
    c = requests.cookies.RequestsCookieJar()
    with open(file_path, encoding='utf-8', mode='r') as f:
        cookies = json.loads(f.read())
        for item in cookies:
            c.set(item["name"], item["value"])
            # xsrf-token为登录是生成的验证码,有的是放在headers里面返回,一般是登录后保存在cookies信息里面的
            if item["name"] == 'XSRF-TOKEN-CM':
                xsrf_token = item["value"]
        s.cookies.update(c)
    result_url = "接口URL地址"
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/json;charset=UTF-8",
        "Accept-Encoding": "gzip, deflate, br",
        # 如果有xsrf-token请求接口时,必须要在headers里面带上,不然接口请求会失败
        "X-XSRF-TOKEN-CM": xsrf_token,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
    }
    body = {"storefronts":["AL","AR","AU","AT","AZ","BE","KH","CA","CL","CO","HR","CZ","DK","EC","EG","FI","FR","DE","GR","HK","HU","IN","ID","IE","IL","IT","JO","KZ","KR","KW","LB","MO","MY","MX","NL","NZ","NO","OM","PK","PE","PH","PL","PT","QA","RO","SA","SG","ZA","ES","SE","CH","TW","TH","UA","AE","GB","US","VN"]}
    # allow_redirects 禁止重定向
    # verify=False 不验证https
    r1 = s.post(url=result_url, headers=headers, json=body, timeout=60, allow_redirects=False, verify=False)
    if r1.status_code == 200:
        ret1 = json.loads(r1.text)
        print(ret1)

0 人点赞