如何用Python3和selenium实现基于cookie的无密码登录示例?

2026-05-28 22:201阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计780个文字,预计阅读时间需要4分钟。

如何用Python3和selenium实现基于cookie的无密码登录示例?

经过两天的研究,终于实现了cookie免密登录功能,其实就是session。特别开心,因为之前在Python爬虫群里问那些大佬,可能他们的回答会让我心寒,自己摸索吧!+ + + 靠自己比靠别人强,为此我总结下。

进过两天的研究终于实现了cookie的免密登录,其实就是session。特别开心,因为在Python爬虫群里问那些大佬,可是他们的回答令我寒心,自己琢磨!!!

靠谁比如靠自己,为此我总结下经验,以免入门的小白再次踩这样的吭。其实网上很多博客写的都比较不详细甚

首先问题自己思考,不懂得去群里问问,然后最重要的要理解事物的本质,只有理解后才能运用它;最后在百度一下把所有相关的博客都点开,一条一条的看一遍总结下规律,比较喽的就舍弃,这样基本上都能得到启发作用。比如selenium的本质就是操作浏览器的,那么操作cookie也是操作浏览器的,并且用selenium打开浏览器时什么都没有,如果进入某个网站就会生成cookie先关信息。等待,那么我们可以用selenium打开网站之后让他清除所有cookie避免干扰,然后睡眠20秒以便等我们登陆,这是就会产生cookie,我们将它获取下来就可以实现登陆了。

下面直接上代码:

如何用Python3和selenium实现基于cookie的无密码登录示例?

# 登录 def login_jd(): # 登录前清除所有cookie browser.get('www.baidu.com/') browser.delete_all_cookies() # 打印登录前的cookie cookieBefore = browser.get_cookies() print(cookieBefore) print("------------------------------------------------------------------------") time.sleep(2) list_cookies = [ {'domain': '.baidu.com', 'zhidao.baidu.com/' #登录前清楚所有cookie driver.delete_all_cookies() driver.get(logurl) ##登录前打印cookie print(driver.get_cookies()) ##点击登录按钮 driver.find_element_by_xpath('//*[@id="userbar-login"]').click() # driver.find_element_by_id("userbar-login").click() time.sleep(2) ##首次尝试的 默认进入扫码登录的界面 try: footerULoginBtn = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]') footerULoginBtn.click() #切换到用户名和密码登录 footerULoginBtn_not_exist = False except: footerULoginBtn_not_exist = True ## 用户名跟密码的设置并点击提交 user = driver.find_element_by_name('userName') user.clear() pwd = driver.find_element_by_name('password') pwd.clear() submit = driver.find_element_by_id('TANGRAM__PSP_10__submit') time.sleep(2) user.send_keys('用户名') pwd.send_keys('密码') time.sleep(1) submit.click() time.sleep(1) ## 发送手机验证码 验证 ##点击发送按钮 ###是否需要输入手机验证码 try: driver.find_element_by_xpath('//*[@id="TANGRAM__28__button_send_mobile"]').click() time.sleep(10) ##使用shell交互式,接受验证码 message = input("Tell me the captcha: ") ##输入验证码 captcha = driver.find_element_by_xpath('//*[@id="TANGRAM__28__input_label_vcode"]') time.sleep(1) captcha.send_keys(message) time.sleep(1) ##点击提交 driver.find_element_by_xpath('//*[@id="TANGRAM__28__button_submit"]').click() time.sleep(3) except: time.sleep(1) ### 获取cookie cookie = driver.get_cookies() print(cookie) jsonCookies = json.dumps(cookie) with open('vcyber.json', 'w') as f: f.write(jsonCookies) time.sleep(30) Crawler.gather()

获取cookie后,可以不用输入密码登录

import json import time from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.common.exceptions import NoSuchElementException class Crawler(): def gather(): chrome_options = Options() chrome_options.add_argument("window-size=1024,768") driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\devtool\Anaconda\Scripts\chromedriver') wait = WebDriverWait(driver, 1) ##登录百度知道 logurl = 'zhidao.baidu.com/' #登录前清楚所有cookie driver.delete_all_cookies() driver.get(logurl) f1 = open('vcyber.json') cookie = f1.read() cookie = json.loads(cookie) for c in cookie: driver.add_cookie(c) # # 刷新页面 driver.refresh() Crawler.gather()

到此这篇关于Python3+selenium实现cookie免密登录的示例代码的文章就介绍到这了,更多相关selenium cookie免密登录内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计780个文字,预计阅读时间需要4分钟。

如何用Python3和selenium实现基于cookie的无密码登录示例?

经过两天的研究,终于实现了cookie免密登录功能,其实就是session。特别开心,因为之前在Python爬虫群里问那些大佬,可能他们的回答会让我心寒,自己摸索吧!+ + + 靠自己比靠别人强,为此我总结下。

进过两天的研究终于实现了cookie的免密登录,其实就是session。特别开心,因为在Python爬虫群里问那些大佬,可是他们的回答令我寒心,自己琢磨!!!

靠谁比如靠自己,为此我总结下经验,以免入门的小白再次踩这样的吭。其实网上很多博客写的都比较不详细甚

首先问题自己思考,不懂得去群里问问,然后最重要的要理解事物的本质,只有理解后才能运用它;最后在百度一下把所有相关的博客都点开,一条一条的看一遍总结下规律,比较喽的就舍弃,这样基本上都能得到启发作用。比如selenium的本质就是操作浏览器的,那么操作cookie也是操作浏览器的,并且用selenium打开浏览器时什么都没有,如果进入某个网站就会生成cookie先关信息。等待,那么我们可以用selenium打开网站之后让他清除所有cookie避免干扰,然后睡眠20秒以便等我们登陆,这是就会产生cookie,我们将它获取下来就可以实现登陆了。

下面直接上代码:

如何用Python3和selenium实现基于cookie的无密码登录示例?

# 登录 def login_jd(): # 登录前清除所有cookie browser.get('www.baidu.com/') browser.delete_all_cookies() # 打印登录前的cookie cookieBefore = browser.get_cookies() print(cookieBefore) print("------------------------------------------------------------------------") time.sleep(2) list_cookies = [ {'domain': '.baidu.com', 'zhidao.baidu.com/' #登录前清楚所有cookie driver.delete_all_cookies() driver.get(logurl) ##登录前打印cookie print(driver.get_cookies()) ##点击登录按钮 driver.find_element_by_xpath('//*[@id="userbar-login"]').click() # driver.find_element_by_id("userbar-login").click() time.sleep(2) ##首次尝试的 默认进入扫码登录的界面 try: footerULoginBtn = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]') footerULoginBtn.click() #切换到用户名和密码登录 footerULoginBtn_not_exist = False except: footerULoginBtn_not_exist = True ## 用户名跟密码的设置并点击提交 user = driver.find_element_by_name('userName') user.clear() pwd = driver.find_element_by_name('password') pwd.clear() submit = driver.find_element_by_id('TANGRAM__PSP_10__submit') time.sleep(2) user.send_keys('用户名') pwd.send_keys('密码') time.sleep(1) submit.click() time.sleep(1) ## 发送手机验证码 验证 ##点击发送按钮 ###是否需要输入手机验证码 try: driver.find_element_by_xpath('//*[@id="TANGRAM__28__button_send_mobile"]').click() time.sleep(10) ##使用shell交互式,接受验证码 message = input("Tell me the captcha: ") ##输入验证码 captcha = driver.find_element_by_xpath('//*[@id="TANGRAM__28__input_label_vcode"]') time.sleep(1) captcha.send_keys(message) time.sleep(1) ##点击提交 driver.find_element_by_xpath('//*[@id="TANGRAM__28__button_submit"]').click() time.sleep(3) except: time.sleep(1) ### 获取cookie cookie = driver.get_cookies() print(cookie) jsonCookies = json.dumps(cookie) with open('vcyber.json', 'w') as f: f.write(jsonCookies) time.sleep(30) Crawler.gather()

获取cookie后,可以不用输入密码登录

import json import time from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.common.exceptions import NoSuchElementException class Crawler(): def gather(): chrome_options = Options() chrome_options.add_argument("window-size=1024,768") driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\devtool\Anaconda\Scripts\chromedriver') wait = WebDriverWait(driver, 1) ##登录百度知道 logurl = 'zhidao.baidu.com/' #登录前清楚所有cookie driver.delete_all_cookies() driver.get(logurl) f1 = open('vcyber.json') cookie = f1.read() cookie = json.loads(cookie) for c in cookie: driver.add_cookie(c) # # 刷新页面 driver.refresh() Crawler.gather()

到此这篇关于Python3+selenium实现cookie免密登录的示例代码的文章就介绍到这了,更多相关selenium cookie免密登录内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!