Python 利用cookie实现自动登录尝试1

代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import json
import time

# 设置用户代理
#chrome_option = Options()
#chrome_option.add_argument(
   # "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
#chrome_option.add_argument("--ignore-certificate-errors")
#chrome_option.add_experimental_option('excludeSwitches', ['enable-automation'])
#chrome_option.add_argument('--disable-blink-features=AutomationControlled')

# 初始化浏览器
#driver1 = webdriver.Chrome(options=chrome_option)

try:
    # 打开登录页面
   # driver1.get("https://passport.csdn.net/login?code=applets")

    # 等待页面加载
   # time.sleep(5)

    # 找到用户名和密码输入框
   # login = driver1.find_element(By.XPATH, "/html/body/div[2]/div/div[2]/div[2]/div[2]/div[1]/div[1]/span[4]")
  #  login.click()
   # input_username = driver1.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[1]/div/input')
   # input_username.click()
   # input_username.send_keys("17888554982")
   # input_pd = driver1.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[2]/div/input')
   # input_pd.click()
   # input_pd.send_keys("Wang02004685")
  #  icon = driver1.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div[2]/div/i')
   # icon.click()
   # submit = driver1.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[4]/button')
  #  submit.click()

    # 等待页面加载,通常需要等待跳转到登录后的页面
  #  time.sleep(10)

    # 获取当前页面的cookies
  #  cookies = driver1.get_cookies()
  #  cookies_json = json.dumps(cookies)
   # with open("cookies.json",'w') as f:
     #   f.write(cookies_json)
      #  print("保存cookie成功")
   # driver1.quit()

    chrome_option = webdriver.ChromeOptions()
    chrome_option.add_argument(
        "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
    chrome_option.add_argument("--ignore-certificate-errors")
    chrome_option.add_experimental_option('excludeSwitches', ['enable-automation'])
    chrome_option.add_argument('--disable-blink-features=AutomationControlled')
    heads = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
    }
    # 初始化浏览器
    driver2 = webdriver.Chrome(chrome_option)

    cookies_json = {}
    url2= "https://passport.csdn.net/login?code=applets"

    with open("cookies.json",'r') as fi:
        cookie_jsons = json.loads(fi.read())

    # driver2得到driver1的cookie,先删除自己的所有cookie,再将driver1的cookie添加进来
    # 必须首先加载网站,这样selenium才知道cookie是属于哪个网站的
    driver2.get(url2)
    # 一旦加载网站,即使没登录,也会产生一个cookie,所以这个cookie被删除了
    driver2.delete_all_cookies()
    # 遍历savedCookies中的两个元素
    for cookie in cookie_jsons:
        # k代表着add_cookie的参数cookie_dict中的键名,这次我们要传入这5个键
        for k in {'name', 'value', 'domain', 'path', 'expiry'}:
            # cookie.keys()属于'dict_keys'类,通过list将它转化为列表
            if k not in list(cookie.keys()):
                # saveCookies中的第一个元素,由于记录的是登录前的状态,所以它没有'expiry'的键名,我们给它增加
                if k == 'expiry':
                    t = time.time()
                    cookie[k] = int(t)  # 时间戳s
        # 将每一次遍历的cookie中的这五个键名和键值添加到cookie
        driver2.add_cookie({k: cookie[k] for k in {'name', 'value', 'domain', 'path', 'expiry'}})

    # 加载我们想要看到的页面的url
    driver2.get(url2)

    time.sleep(5)

    # 访问目标页面
   # driver.get(
     #   "https://blog.csdn.net/m0_62645012/article/details/139565489?spm=1001.2100.3001.7377&utm_medium=distribute.pc_feed_blog_category.none-task-blog-classify_tag-1-139565489-null-null.nonecase&depth_1-utm_source=distribute.pc_feed_blog_category.none-task-blog-classify_tag-1-139565489-null-null.nonecase")

    # 等待页面加载
   # time.sleep(5)

    # 保存截图
    driver2.save_screenshot('login.png')

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # 关闭浏览器
    driver2.quit()
  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值