from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import json
s = Service(r"D:\untitled2\venv\Scripts\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.implicitly_wait(3)
driver.get("https://mycaifuhao.eastmoney.com/ipub/home/Index?type=mycontent")
time.sleep(60)
# 在等待的过程中输入账号密码,记录下此时的cookie值
path = "C:/Users/MrLuo/Desktop/数据文件/量化交易/cookies.txt"
with open(path, 'w') as f:
# 将cookies保存为json格式
f.write(json.dumps(driver.get_cookies()))
quit()
s = Service(r"D:\untitled2\venv\Scripts\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.implicitly_wait(3)
driver.get("https://mycaifuhao.eastmoney.com/ipub/home/Index?type=mycontent")
# 首先清除由于浏览器打开已有的cookies
driver.delete_all_cookies()
with open('C:/Users/MrLuo/Desktop/数据文件/量化交易/cookies.txt', 'r') as f:
# 使用json读取cookies 注意读取的是文件 所以用load而不是loads
cookies_list = json.load(f)
for cookie in cookies_list:
driver.add_cookie(cookie)
driver.refresh()
print("登录成功")
python 通过cookies实现自动登录
最新推荐文章于 2024-07-17 20:12:36 发布