12306模擬登錄-超級鷹

12306模擬登錄-超級鷹

什麼是超級鷹?

就是和雲打碼相似的驗證碼識別網站,可是他能識別更復雜的圖片驗證碼html

模擬登錄12306

主要思路web

1.首先登錄到12306界面chrome

2.點擊帳號密碼登陸,到帳號密碼登陸模塊app

3.截取整張界面的圖片保存到本地,並獲取驗證碼部分的座標網站

4.在界面截圖的基礎上,根據獲取的座標截取驗證碼的圖片保存到本地ui

5.把圖片交給超級鷹,返回結果url

6.把結果解析成想要點擊的座標code

7.輸入帳號密碼,執行驗證碼點擊,點擊登陸orm

8.ok,可是有概率沒法識別驗證碼htm

from selenium import webdriver
import chaojiying as cj
import time
#動做鏈,點擊驗證碼
from selenium.webdriver import ActionChains
#圖片處理,截圖用
from PIL import Image
#實現規避檢測
from selenium.webdriver import ChromeOptions
#12306登陸url
url="https://kyfw.12306.cn/otn/resources/login.html"

#規避檢測
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])

chrome=webdriver.Chrome(executable_path="chromedriver",options=option)
chrome.maximize_window()

#發起請求
chrome.get(url)
time.sleep(3)

#切換到帳號密碼登陸
#找到切換按鈕
button=chrome.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a')
#點擊切換到帳號密碼登陸
button.click()
time.sleep(5)

#獲取驗證碼圖片的標籤對象
code_img=chrome.find_element_by_xpath('//*[@id="J-loginImg"]')
#獲取驗證碼圖片的左上角x,y座標位置
img_location=code_img.location
#獲取驗證碼圖片的長寬
img_size=code_img.size
#圖片左上角和右下角的座標
rangle=(
    int(img_location['x']), int(img_location['y']),int(img_location['x'] + img_size['width']),int(img_location['y'] + img_size['height'])
)
time.sleep(2)

#截取整張界面的圖片
chrome.save_screenshot("12306.png")
html_img=Image.open("12306.png")
time.sleep(2)

#設置截圖後的名字
code_img_name="code.png"
#截取驗證碼部分的圖片
frame=html_img.crop(rangle)
#保存驗證碼截圖
frame.save(code_img_name)
time.sleep(2)

#將圖片發給超級鷹識別獲取結果,zx爲封裝的接口
result=cj.zx()

#解決返回結果,建立點擊列表
all_location=[]
if '|' in result['pic_str']:
    lo_list=result['pic_str'].split('|')
    for i in lo_list:
        lo_list=[]
        x=int(i.split(',')[0])
        y=int(i.split(',')[1])
        lo_list.append(x)
        lo_list.append(y)
        all_location.append(lo_list)
else:
    lo_list = []
    x=int(result['pic_str'][0])
    y=int(result['pic_str'][1])
    lo_list.append(x)
    lo_list.append(y)
    all_location.append(lo_list)


for i in all_location:
    x=i[0]
    y=i[1]
    ActionChains(chrome).move_to_element_with_offset(code_img,x,y).click().perform()
    time.sleep(1)

#輸入帳號
chrome.find_element_by_id('J-userName').send_keys("******")
#輸入密碼
chrome.find_element_by_id('J-password').send_keys('******')
time.sleep(2)

#點擊登陸
chrome.find_element_by_id('J-login').click()
time.sleep(10)

chrome.quit()
相關文章
相關標籤/搜索