验证码破解 | Selenium模拟登陆12306
12306官网登录的验证码破解比较简单,验证码是常规的点触类型验证码,使用超级鹰识别率比较高。
思路:
(1)webdriver打开浏览器;
(2)先对整个屏幕截屏,通过标签定位找到验证码图片,并定位到验证码图片的坐标,然后从先前截屏的图片中截取验证码部分的图片;
(3)通过超级鹰识别验证码上字的坐标;
(4)点击验证码图片上的字;
(5) 输入用户名和密码进行登录;
注意:将以下标红部分的账号等信息换成自己的即可成功
from selenium import webdriver from selenium.webdriver import ActionChains from chaojiying import Chaojiying from PIL import Image import time # 用户名和密码是自己的12306账号的用户名和密码 USERNAME = PASSWORD = class Huochepiao(): def __init__(self): self.bro = webdriver.Chrome() self.url = 'https://kyfw.12306.cn/otn/login/init' def open_browser(self): self.bro.get(self.url) time.sleep(5) # def __del__(self): # self.bro.quit() def find_code_img(self): # 定位到验证码图片对应的img标签 code_img = self.bro.find_element_by_class_name('touclick-img-par') location = code_img.location size = code_img.size return (location,size) def get_code_img(self,location,size): # rangle对应的就是验证码图片的裁剪区域 rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) self.bro.save_screenshot('aa.png') i = Image.open('./aa.png') frame = i.crop(rangle) # 根据指定区域实现裁剪 frame.save('code.png') def get_words_locations(self): chaojiying = Chaojiying(username, password, softID) # 超级鹰账号、密码和ID更换即可 im = open('./code.png', 'rb').read() print("超级鹰识别结果:",chaojiying.post_pic(im, 9004)) result = chaojiying.post_pic(im, 9004)['pic_str'] print("result:",result) # "x1,y1|x2,y2" --> [['x1','y1'],['x2','y2']] "x,y" --> [['x','y']] all_coorodinates = [] if '|' in result: li = result.split('|') count = len(li) for i in range(count): xy_list = [] x = int(li[i].split(',')[0]) y = int(li[i].split(',')[1]) xy_list.append(x) xy_list.append(y) all_coorodinates.append(xy_list) else: x = int(result.split(',')[0]) y = int(result.split(',')[1]) xy_list = [] xy_list.append(x) xy_list.append(y) all_coorodinates.append(xy_list) print(all_coorodinates) return all_coorodinates def touch_click_words(self, coorodinates): code_img = self.bro.find_element_by_class_name('touclick-img-par') for coorodinate in coorodinates: x = coorodinate[0] y = coorodinate[1] ActionChains(self.bro).move_to_element_with_offset(code_img, x, y).click().perform() def login(self): self.bro.find_element_by_id('username').send_keys(USERNAME) self.bro.find_element_by_id('password').send_keys(PASSWORD) self.bro.find_element_by_id('loginSub').click() time.sleep(10) def run(self): # 1 打开浏览器 self.open_browser() # 2 找到并获取验证码图片 location,size = self.find_code_img() self.get_code_img(location,size) # 3 识别验证码上字的坐标 all_coorodinates = self.get_words_locations() # 4 点击验证码图片上的字 self.touch_click_words(all_coorodinates) # 5 登录 self.login() if __name__ == "__main__": hcp = Huochepiao() hcp.run()
输出结果:
result: 186,86
[[186, 86]]
注意:本篇博文仅供学习交流相关的爬虫知识,请勿过度使用,如有任何纠纷,与本人无关。(瑟瑟发抖)