(编辑:jimmy 日期: 2024/12/27 浏览:2)
首先是获取cookies保存到本地
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/12/20 11:00 # @Author : huni # @File : cookies获取.py # @Software: PyCharm from selenium import webdriver from time import sleep import json if __name__ == '__main__': driver = webdriver.Chrome(executable_path='./chromedriver.exe') driver.maximize_window() driver.get('https://passport.csdn.net/login"htmlcode">#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/12/20 11:10 # @Author : huni # @File : 使用cookies登录csdn.py # @Software: PyCharm from selenium import webdriver import json def browser_initial(): browser = webdriver.Chrome(executable_path='./chromedriver.exe') browser.maximize_window() browser.get( 'https://www.csdn.net/') return browser def log_csdn(browser): with open('csdn_cookies.txt', 'r', encoding='utf8') as f: listCookies = json.loads(f.read()) # 往browser里添加cookies for cookie in listCookies: cookie_dict = { 'domain': '.csdn.net', 'name': cookie.get('name'), 'value': cookie.get('value'), "expires": '', 'path': '/', 'httpOnly': False, 'HostOnly': False, 'Secure': False } browser.add_cookie(cookie_dict) browser.refresh() # 刷新网页,cookies才成功 if __name__ == "__main__": browser = browser_initial() log_csdn(browser)cookies是有时间限制的,定期获取即可,登陆之后可以进行其他自动化操作,比如给别人的博客评论,点赞等等。