驗證碼破解 | 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]]

 

 

注意:本篇博文僅供學習交流相關的爬蟲知識,請勿過度使用,如有任何糾紛,與本人無關。(瑟瑟發抖)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM