天天看點

Python Selenium自動登入12306官網超級鷹超級鷹功能超級鷹使用步驟12306官網模拟登入:

超級鷹

Python Selenium自動登入12306官網超級鷹超級鷹功能超級鷹使用步驟12306官網模拟登入:
超級鷹官網 http://www.chaojiying.com/

超級鷹功能

1.超級鷹圖檔分類及識别錄入系統

2.獨立的資料及統計服務,實時與總部資料中心直連。

3.整合雲錄入用戶端,提供更高的安全性及工作效率。

4.采用世界先進的圖檔處理算法以及神經網絡訓練系統來提供識别率

超級鷹使用步驟

1.注冊:普通使用者
Python Selenium自動登入12306官網超級鷹超級鷹功能超級鷹使用步驟12306官網模拟登入:

2.登入:普通使用者

3.題分查詢:充值(做實驗充值1塊錢就夠)

Python Selenium自動登入12306官網超級鷹超級鷹功能超級鷹使用步驟12306官網模拟登入:
4.建立一個軟體(id)
Python Selenium自動登入12306官網超級鷹超級鷹功能超級鷹使用步驟12306官網模拟登入:
5.下載下傳示例代碼
Python Selenium自動登入12306官網超級鷹超級鷹功能超級鷹使用步驟12306官網模拟登入:
Python Selenium自動登入12306官網超級鷹超級鷹功能超級鷹使用步驟12306官網模拟登入:

12306官網模拟登入:

編碼流程

1.使用selenium打開登入頁面

2.對目前selenium打開的這張頁面進行截圖

3.對目前圖檔局部區域(驗證碼圖檔)進行裁剪(作用:将驗證碼圖檔和模拟登入進行一一對應。)

4.使用超級鷹識别驗證碼圖檔(坐标)

5.使用動作鍊根據坐标實作點選操作

6 錄入使用者名密碼,點選登入按鈕實作登入

編寫代碼

#超級鷹代碼(直接拷貝即可)
import requests
from hashlib import md5

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode('utf8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, im, codetype):
        """
        im: 圖檔位元組
        codetype: 題目類型 參考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:報錯題目的圖檔ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()

#使用selenium打開登入頁面

from selenium import webdriver
import time
from PIL import Image    #裁剪圖檔子產品
from selenium.webdriver import ActionChains   #動作鍊

bro = webdriver.Chrome(executable_path='./chromedriver')
# 浏覽器最大化,也可以不設定
bro.maximize_window()
#現在使用這個url位址
bro.get('https://kyfw.12306.cn/otn/resources/login.html')
time.sleep(1)
bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()  #使用賬号登入
time.sleep(1)

#save_screenshot就是将目前頁面進行截圖且儲存
bro.save_screenshot('aa.png')

#确定驗證碼圖檔對應的左上角和右下角的坐标(裁剪的區域就确定)
code_img_ele = bro.find_element_by_xpath('//*[@id="J-loginImg"]')

location = code_img_ele.location  # 驗證碼圖檔左上角的坐标 x,y
print('location:',location)
size = code_img_ele.size  #驗證碼标簽對應的長和寬
print('size:',size)

#左上角和右下角坐标
rangle = (
int(location['x']), int(location['y']), int(location['x']+size['width'] ), int(location['y']+size['height'] ))

#至此驗證碼圖檔區域就确定下來了
i = Image.open('./aa.png')
code_img_name = './code.png'
#crop根據指定區域進行圖檔裁剪
frame = i.crop(rangle)
frame.save(code_img_name)

#将驗證碼圖檔送出給超級鷹進行識别
chaojiying = Chaojiying_Client('超級鷹使用者名', '超級鷹密碼', '軟體ID')   #使用者中心>>軟體ID 生成一個替換 96001
im = open('code.png', 'rb').read()  #本地圖檔檔案路徑 來替換 a.jpg 有時WIN系統須要//
print(chaojiying.PostPic(im, 9004)['pic_str'])
result = chaojiying.PostPic(im, 9004)['pic_str']
all_list = [] #要存儲即将被點選的點的坐标  [[x1,y1],[x2,y2]]

if '|' in result:
    list_1 = result.split('|')
    count_1 = len(list_1)
    for i in range(count_1):
        xy_list = []
        x = int(list_1[i].split(',')[0])
        y = int(list_1[i].split(',')[1])
        xy_list.append(x)
        xy_list.append(y)
        all_list.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_list.append(xy_list)
print(all_list)
#周遊清單,使用動作鍊對每一個清單元素對應的x,y指定的位置進行點選操作
for l in all_list:
    x = l[0]
    y = l[1]
    ActionChains(bro).move_to_element_with_offset(code_img_ele, x, y).click().perform()
    time.sleep(1)

bro.find_element_by_id('J-userName').send_keys('使用者名')
time.sleep(1)
bro.find_element_by_id('J-password').send_keys('密碼')
bro.find_element_by_id('J-login').click()
time.sleep(1)
div=bro.find_element_by_id('nc_1_n1z')

#動作鍊
action = ActionChains(bro)
#點選長按指定的标簽
action.click_and_hold(div)

#處理滑動子產品
for i in range(5):
    #perform()立即執行動作鍊操作
    #move_by_offset(x,y):x水準方向 y豎直方向
    action.move_by_offset(30,0).perform() #速度為30mm
    sleep(0.5)

#釋放動作鍊
action.release()
           

運作效果:

成功登入

注意事項: