1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python+selenium获取cookie session_Python Selenium模拟登录成功后 使用此cookie

python+selenium获取cookie session_Python Selenium模拟登录成功后 使用此cookie

时间:2022-05-03 08:42:14

相关推荐

python+selenium获取cookie session_Python Selenium模拟登录成功后 使用此cookie

一. 步骤概述

a. 模拟登录学校选课系统(使用Selenium库登陆http://xk./xsxk/login.xk)

b. 取得cookie后传入requests的session中。(参考博客:/big__v/article/details/78151940)

c. 使用requests库中的post提交选课序号至http://xk./xsxk/xkOper.xk

d. 完成选课

二. 手动登录时浏览器截图(Chrmoe)

1. 登录时request headers(这里有一个cookie就是之后会出错的地方。)

2. 登录时post

三. 出错情况概述

1. 如果手动用浏览器登录并复制上述cookie直接进入get的headers里,get命令能够成功完成,并提示选课成功。

2. 如果把手动用浏览器登录这一步用Selenium模拟登录代替,并直接将cookies传入requests的session中,再次尝试gett就会报出“非法进入”。

四. 问题排除

1. 第二步get推测没有问题,因为复制浏览器中cookie直接进入get的headers里,post命令能够成功完成。

2. 不是cookie格式问题,因为格式问题会导致报错“登录过期”,而不是“非法登录”。

五. 问题猜测

猜测可能cookie进行过加密

猜测存在其他反爬虫手段

猜测cookie存在时效性

六. 完整代码如下

# -*- coding:utf-8 -*-

from selenium import webdriver

import requests

from selenium.webdriver.support.wait import WebDriverWait

# 添加选课序号

codes = []

while True:

code = raw_input(u'请输入选课序号(输入回车结束):')

if code != '':

codes.append(code)

else:

break

# 开始登录

driver = webdriver.Chrome()

url = "http://xk./xsxk/login.xk"

s = requests.session()

while True:

driver.get(url)

name_input = driver.find_element_by_id('username') # 找到用户名的框框

pass_input = driver.find_element_by_id('password') # 找到输入密码的框框

login_button = driver.find_element_by_xpath('//*[@id="loginForm"]/table/tbody/tr[4]/td[2]/input[1]')

name_input.clear()

name_input.send_keys('1*******') # 填写用户名

pass_input.clear()

pass_input.send_keys('********') # 填写密码

WebDriverWait(driver, 300000000).until_not(lambda x: x.find_element_by_id("verifyCode").is_displayed()) # 等待直到登录成功

if u'夜大学' in driver.page_source:

print u"登录成功!"

selenium_cookies = driver.get_cookies() # 把selenium获取的cookies保存到变量,备用。

# print(selenium_cookies)

driver.close()

break

else:

driver.close()

# 处理cookie

s = requests.Session()

for i in selenium_cookies:

requests.utils.add_dict_to_cookiejar(s.cookies, {i['name']: i['value']})

# 以下用post提交选课

for code in codes:

info = {'method': 'handleQxgxk',

'jxbid': '1' + code,

'glJxbid': '',

'xyjc': ''}

r = s.get('http://xk./xsxk/xkOper.xk', params=info)

if 'false' in r.text:

print '选课序号:%s,选课失败。' % code, r.text

else:

print '选课序号:%s,选课成功。' % code

python+selenium获取cookie session_Python Selenium模拟登录成功后 使用此cookie 利用requests库进行get时 提示“非法登陆”。...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。