# 下面是12306 實現的模擬登錄php
# 解碼 應用超級鷹,註冊用戶,左側欄軟件ID進去,開啓一個新軟件,拿到軟件IDweb
# 下面測試都在jupyter裏面實現chrome
# 超級鷹類 cell
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):
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)
print(r.json()) #{'err_no': 0, 'err_str': 'OK', 'pic_id': '9067216592371000003', 'pic_str': '24,62|40,146|112,141', 'md5': 'c4ee4d4fb269521c47de228d5c6d6d3e'}
return r.json()
def ReportError(self, im_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()
# 下面是12306 頁面的處理json
from selenium import webdriver import time import requests from lxml import etree from urllib import request from selenium.webdriver.common.action_chains import ActionChains from PIL import Image bro = webdriver.Chrome(executable_path='./chromedriver.exe') bro.get('https://kyfw.12306.cn/otn/login/init') # page_text = bro.page_source # tree = etree.HTML(page_text) # code_img_src = tree.xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img/@src')[0] # print(code_img_src) # request.urlretrieve(url=code_img_src,filename='./code.jpg') time.sleep(3) code_img_ele = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img') #找見驗證碼區域 time.sleep(3) location = code_img_ele.location # x,y print('--',location) # -- {'x': 274, 'y': 274} size = code_img_ele.size print('++',location) # ++ {'x': 274, 'y': 274} rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #驗證碼圖裁剪區域 矩形 print('**',rangle) #矩形 四點 像素值 bro.save_screenshot('aa.png') # 快照 整張頁面截圖 # 指定區域的截圖
i = Image.open('./aa.png') #打開圖片 code_img_name = 'code.png' #最終驗證碼裁剪出來的保存名稱 frame = i.crop(rangle) #裁剪 frame.save(code_img_name) #裁剪成功的圖片保存 # 將驗證碼圖片提交給 超級鷹 處理 先實例化 chaojiying = Chaojiying_Client('超級鷹帳號', '超級鷹密碼', '軟件ID')#超級鷹用戶中心>>軟件ID 生成一個替換上 im = open('./code.png','rb').read() #讀取驗證碼圖片 result = chaojiying.PostPic(im, 9004)['pic_str'] # 返回座標 # 'x1,y1|x2,y2' 'x,y' result值 轉換成[['x','y']] [['x1','y1'],['x2','y2']] all_list = [] 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]) print(x) y = int(result.split(',')[1]) xy_list = [] xy_list.append(x) xy_list.append(y) all_list.append(xy_list) print(all_list) # [[24, 62], [40, 146], [112, 141]] 座標位置 #code_img = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img') action = ActionChains(bro) #動做鏈 實例化一個對象 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() bro.find_element_by_id('username').send_keys('') #12306帳號 time.sleep(2) bro.find_element_by_id('password').send_keys('') #12306密碼 time.sleep(2) bro.find_element_by_id('loginSub').click() time.sleep(10) bro.quit() # 退出