我为什么要加多个requests

在这次改进方案中,selenium主要承担的还是识别网页的title,进行一次判断。在上一篇文章中我也有提出,我的代码中的登陆后识别的部分代码是有缺陷的。今天突然想了想,可以采用更简单的方式去进行验证,那就是访问百度首页 ,对于访问这种简单网站,用selenium重新开一个窗口工程量稍微有点大,于是采用了requests来写这段内容,一下子就可以将selenium要很长处理的内容用短短两行完全表达了出来。

r = requests.get('http://www.baidu.com')if r.status_code == 200:

status_code是网页解析上可以看到的,关于具体的解析可以翻看我。其中status_code=200是常用的,这表示的是网页可以正常访问。

检测主要部分代码

def Is_OK():global IsCode#判断跳转界面driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent')if page_information == '认证成功页':#判断联网r = requests.get('http://www.baidu.com')#print(r.status_code)if r.status_code == 200:IsCode = 0print("login successfully")else:IsCode = 1quit()login(account, password)elif page_information == '信息页':print("出现信息页,正在返回准备重新登录")back()login(account, password)IsCode = 1elif page_information == '上网登录窗':login(account, password)IsCode = 1return IsCode

可以看出,我这里采用的是页面验证+访问认证的方式。 如果你的电脑显示出了认证成功页,我才需要去检测是否联网;如果你的电脑还是显示的是信息页或者是上网登录窗,那么我暂时不需要检测联网,因为代码根本还没有帮你完成你所需要的东西。 关于这种思路,我也运用到了一开始的三个窗口识别中,检测如果你的认证呢成功页也是有网的话,我就主动退出了程序,这样节省大家的时间。

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

python免费学习资料以及群交流解答点击即可加入

检测的循环

检测我是用到了一个while循环,因为不知道具体要循环多少次才能检测成功,但我是设置了最多检测4次。在实际使用情况下,最多需要检测的次数只有2次,需要检测到第四次的时候,说明要不就是半夜断网的时刻或者就是你的账号密码输错了的情况,这种情况就需要你去进行判断了。 当然,关于账号密码输错的这种情况,除了手动删除代码生成的txt文件之外,还可以在代码中设置一些手段,在检测四次之后,自动返回之前,然后让你重新输入账号密码,覆盖本地文件,这种情况由于出现次数太少,我就没有写了,有兴趣的朋友可以研究研究,在这段代码中加入。

全部代码

#login_selenium.py
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time, os, requests
'''
NJUPT-CMCC/ChinaNet自动登录代码
author:海hong
本功能主要实现了对于登陆页面三个窗口的识别以及账号密码保存在本地的功能。
通过第一次登陆需要输入账号密码,以后登录就是一键便可完成,时间也很快,目前测试暂时没见bug。
'''
def txt_handle():global account, passwordtry:f = open('校园网登录信息文件.txt', "r", encoding="utf-8")lines = f.readlines()print("已进入")account = lines[0]account = account.replace('\n', '')password = lines[1]f.close()#print(account, password)except Exception as e:account = input("请输入账号:")password = input("请输入密码:")txt = account + '\n' + passwordprint(account, password)f = open('校园网登录信息文件.txt', "w+", encoding="utf-8")f.write(txt)print("文件信息已写入,请勿删除该文件!")f.close()return account, passworddef login(account, password):#print(account, password)input_account = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(3)')))input_password = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(4)')))button = driver.find_element_by_css_selector('#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(2)')input_account[0].send_keys(account)input_password[0].send_keys(password)#print("准备点击")button.click()def back():button_back = driver.find_element_by_css_selector('#edit_body > div > div.edit_loginBox.ui-resizable-autohide > form > input')button_back.click()def quit():button_quit = driver.find_element_by_css_selector('#edit_body > div > div.edit_loginBox.ui-resizable-autohide > form > input')button_quit.click()time.sleep(2)confirm = driver.switch_to.alertconfirm.accept()print('你刚刚确认下线')time.sleep(5)confirm.accept()print('现在返回上网登录窗页面')def Is_OK():global IsCode#判断跳转界面driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent')if page_information == '认证成功页':#判断联网r = requests.get('http://www.baidu.com', timeout =30)#print(r.status_code)if r.status_code == 200:IsCode = 0print("login successfully")else:IsCode = 1quit()login(account, password)elif page_information == '信息页':print("出现信息页,正在返回准备重新登录")back()login(account, password)IsCode = 1elif page_information == '上网登录窗':login(account, password)IsCode = 1return IsCodedef Is_page():#判断跳转界面driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent')if page_information == '上网登录窗':login(account, password)elif page_information == '信息页':back()login(account, password)elif page_information == '认证成功页':r = requests.get('http://www.baidu.com', timeout =30)if r.status_code == 200:IsCode = 0print("已登陆成功,无需重复登陆")else:quit()login(account, password)#检测页面是否跳转到认证成功页&能否正常上网count = 0while IsCode != 0:count = count + 1Is_OK()if IsCode == 1:print("第%s次验证无法通过"%str(count))else:print("第%s次验证通过"%str(count))if count == 4:print("验证次数过多,请注意是否断网或者账号密码出错!")breakurl = 'http://p.njupt.edu.cn'
driver = webdriver.Chrome('chromedriver.exe')
wait = WebDriverWait(driver, 30)
driver.get(url)
account = 'XXXXXXXXXX'
password = 'XXXXXX'
IsCode = 1
txt_handle()
#print(account, password)Is_page()driver.quit()
print("5秒后窗口自动关闭")
time.sleep(4)
os.sys.exit()

selenium+requests实现自动连接校园网并验证!相关推荐

  1. Linux 服务器自动连接校园网,selenium + geckodriver + firefox浏览器

    文章目录 设备信息 一. 下载安装 firefox 浏览器相关驱动 1. firefox 浏览器版本查看 2. 下载对应版本的 selenium 和 geckodriver 驱动 二. 自动连接脚本 ...

  2. 【Ubuntu】开启ssh服务/配置ftp内网穿透/自动连接校园网

    前言 想让工作电脑开启ssh服务,这样就可以在校外进行远程访问办公,电脑的系统为Ubuntu20.04 开启ssh服务 首先查看当前Ubuntu安装的SSH服务: dpkg -l | grep ssh ...

  3. Selenium使用浏览器自动登录校园网

    Selenium使用浏览器自动登录校园网 标签: python 爬虫 校园网需要登录验证,所以采用Selenium操作浏览器直接登录. 打开浏览器 from selenium import webdr ...

  4. Python脚本-自动连接校园网

    自动连接校园网 使用语言:Python python爬虫 浏览器:谷歌浏览器 import requests#登录地址 URL="http://10.2.255.26:801/eportal ...

  5. 南信大电脑开机自动连接校园网

    2022-11-20更新:南信大更新了连接时的url,更改部分如下: val url ="http://10.255.255.46/api/v1/login"val ipPath ...

  6. 1.1.1.1校园网_还担心断网?快试试清华校园网自动连接程序

    作为一名热爱科ban研zhuan的博士生,当我放假回家的时候,最挂念的就是实验室电脑上的文件和程序.在校外无法通过mstsc使用IP直连,只能通过第三方远程连接软件诸如Teamviewer.向日葵等来 ...

  7. 手把手教你python实现校园网自动连接,零基础也可以轻松实现

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 目录 前言 一.准备工具 二.具体步骤 1.创建项目和安装request库 a.创建项目 b.安装request库 2.编写程序 a. ...

  8. 解决清华大学校园网自动连接问题

    面向问题: 当学校放假或长时间离开实验的时候,需要远程控制台式机或服务器(对于远程操作,推荐使用Microsoft Remote Desktop).但是因为清华大学校园网非常靠谱,一段时间没有操作或者 ...

  9. 路由器连接校园网并发WIFI:WR703N路由器安装OpenWRT并运行H3C客户端操作步骤(主要针对中山大学东校区)

    注意:本文所有的最新更正请全部前往http://blog.reetsee.com/archives/227查看. 本文主要目的在于让你的路由器能自动连接学校的校园网并且能发出WIFI让所有设备使用.要 ...

最新文章

  1. 多线程读一个全局变量要不要加锁?还是说只是当修改全局变量的时候才要加锁?...
  2. python判断字符串是否是数字字母
  3. @RequestBody, @ResponseBody 注解详解
  4. 012_原始值和引用值
  5. 2017年10月21日普及组 简单单词
  6. 拾取模型的原理及其在THREE.JS中的代码实现
  7. python 保存图片代码_最简单的selenium+Python自动右键保存图片
  8. ubuntu 18.04 ip固定
  9. Python使用颜色块覆盖视频中指定区域的内容
  10. 10532: 花 [dp]
  11. php去除emoji表情代码
  12. Ubuntu-区域截图
  13. Linux内核移植之DM9000网卡驱动
  14. 计算机博弈之国际跳棋入门-规则篇
  15. 10、(十)外汇交易中专有名词整理
  16. 三菱FX系列PLC与三菱变频器通讯应用实例
  17. slf4j的包使用说明
  18. Excel数据透视表按指定文字顺序排序方法
  19. Goroutine及其使用实例【Go语言圣经笔记】
  20. 使用itext加图片水印或文字水印

热门文章

  1. L1-038 新世界 (5 分)
  2. 【转载】Hint的常见错误使用方式
  3. 网站代码该如何优化?
  4. 计算机启动bios设置程序,电脑开机显示没有检测到开机设备系统会进入BIOS设置程序,怎么处理 这是什么情况...
  5. 微信小程序-从注册到上架
  6. 进大厂就一定要考研吗?答案毫无疑问是否定的
  7. Kaggle课程 — 机器学习进阶 Intermediate Machine Learning
  8. 台式计算机可以接收无线不,台式电脑无线接收器插上连不上网怎么处理
  9. 把数字翻译成字符串python_把数字翻译成字符串
  10. 比尔·盖茨重申去世20年后关闭基金会:那时的富人更懂慈善