环境准备
- Python3
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32 - Selenium
python3 -m pip install selenium - Firefox
http://ftp.mozilla.org/pub/firefox/releases/78.0b2/ - geckodriver
https://github.com/mozilla/geckodriver/releases/tag/v0.26.0
操作步骤
- 运行脚本
__author__ = 'vito'
from selenium import webdriver
import time
import json
#填写webdriver的保存目录
driver = webdriver.Firefox(executable_path='./geckodriver.exe')
#记得写完整的url 包括http和https
driver.get('https://www.baidu.com')
time.sleep(20) # 需要在这段时间内完成手动登录
with open('cookies_baidu.txt', 'w') as fp:
fp.write(json.dumps(driver.get_cookies()))
driver.close()
- 手动登录,打开的浏览器页面输入用户名、密码和验证码,登录成功
- 查看脚本目录生成的txt文件