首先运行代码 1.py
并手动登入用户账号,
然后程序自动保存与用户相关的cookies文件,
并将cookies内容以pickle的形式保存到当前目录下.
最后运行 2.py 文件,即可实现用户登入.
1.py
from selenium import webdriver
import time
import pickle
browser = webdriver.Chrome()
browser.get('https://www.bilibili.com/')
time.sleep(5)
print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())
input("请输入任意键继续...")
time.sleep(5)
with open("cookies文件.pickle",'wb') as file:
pickle.dump(browser.get_cookies(),file)
print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())
input("请输入任意键继续...")
browser.delete_all_cookies()
time.sleep(5)
print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())
2.py
from selenium import webdriver
import time
import pickle
with open("cookies文件.pickle",'rb') as file:
cookiesList = pickle.load(file)
browser = webdriver.Chrome()
browser.get('https://www.bilibili.com/')
input("请输入任意键继续...")
for cookie in cookiesList:
browser.add_cookie(cookie)
browser.get('https://www.bilibili.com/')
time.sleep(1)
# print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())
input("请输入任意键继续...")
browser.quit()
生成pickle文件以保存cookies:
运行效果: