1 from hashlib import md5 2 from selenium import webdriver 3 import time 4 import requests 5 from selenium.webdriver import ActionChains 6 from PIL import Image 7
8
9 class Chaojiying_Client(object): 10
11 def __init__(self, username, password, soft_id): 12 self.username = username 13 password = password.encode('utf8') 14 self.password = md5(password).hexdigest() 15 self.soft_id = soft_id 16 self.base_params = { 17 'user': self.username, 18 'pass2': self.password, 19 'softid': self.soft_id, 20 } 21 self.headers = { 22 'Connection': 'Keep-Alive', 23 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)', 24 } 25
26 def PostPic(self, im, codetype): 27 """
28 im: 圖片字節 29 codetype: 題目類型 參考 http://www.chaojiying.com/price.html 30 """
31 params = { 32 'codetype': codetype, 33 } 34 params.update(self.base_params) 35 files = {'userfile': ('ccc.jpg', im)} 36 r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, 37 headers=self.headers) 38 return r.json() 39
40 def ReportError(self, im_id): 41 """
42 im_id:報錯題目的圖片ID 43
44
45 """
46 params = { 47 'id': im_id, 48 } 49 params.update(self.base_params) 50 r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers) 51 return r.json() 52
53
54 bro = webdriver.Chrome() 55 bro.get('https://kyfw.12306.cn/otn/login/init') 56
57 time.sleep(3) 58 # 定位到驗證碼圖片對應的img標籤
59 code_img_ele = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img') 60
61 time.sleep(3) 62 # 上一步定位到驗證碼圖片標籤的位置座標(左上角的座標)
63 location = code_img_ele.location # x,y
64 # size返回的就是驗證按摩圖片的尺寸
65 size = code_img_ele.size # width and height
66 # rangle對應的就是驗證碼圖片的裁剪區域
67 rangle = ( 68 int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) 69
70 # save_screenshot將當前瀏覽器打開的頁面進行總體截圖
71 bro.save_screenshot('aa.png') 72 # 進行指定區域的截圖
73 i = Image.open('./aa.png') 74 # 驗證碼圖片的名稱
75 code_img_name = 'code.png'
76 # 根據指定區域實現裁剪
77 frame = i.crop(rangle) 78 frame.save(code_img_name) 79
80 # 將驗證碼圖片提交給超級鷹進行識別
81 chaojiying = Chaojiying_Client('328410948', '328410948', '899370') # 用戶中心>>軟件ID 生成一個替換 96001
82 im = open('./code.png', 'rb').read() 83 result = chaojiying.PostPic(im, 9004)['pic_str'] 84 # "x1,y1|x2,y2" "x,y"
85
86 # [['x1','y1'],['x2','y2']]
87 # [['x','y']]
88
89 all_list = [] 90 if '|' in result: 91 list_1 = result.split('|') 92 count_1 = len(list_1) 93 for i in range(count_1): 94 xy_list = [] 95 x = int(list_1[i].split(',')[0]) 96 y = int(list_1[i].split(',')[1]) 97 xy_list.append(x) 98 xy_list.append(y) 99 all_list.append(xy_list) 100 else: 101 x = int(result.split(',')[0]) 102 y = int(result.split(',')[1]) 103 xy_list = [] 104 xy_list.append(x) 105 xy_list.append(y) 106 all_list.append(xy_list) 107 print(all_list) 108
109 code_img = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img') 110
111 action = ActionChains(bro) 112
113 for l in all_list: 114 x = l[0] 115 y = l[1] 116 ActionChains(bro).move_to_element_with_offset(code_img, x, y).click().perform() 117
118 bro.find_element_by_id('username').send_keys('xxxxxx') 119 time.sleep(2) 120 bro.find_element_by_id('password').send_keys('xxxxx') 121 time.sleep(2) 122 bro.find_element_by_id('loginSub').click() 123 time.sleep(10) 124 bro.quit()