selenium自動登入12306
阿新 • • 發佈:2020-09-17
selenium自動登入12306
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() # chaojiying = Chaojiying_Client('pachong', '827965', '908119') #使用者中心>>軟體ID 生成一個替換 96001 # im = open('code.png', 'rb').read() #本地圖片檔案路徑 來替換 a.jpg 有時WIN系統須要// # print( chaojiying.PostPic(im, 9004)['pic_str']) from selenium import webdriver from time import sleep # 規避被檢測 from selenium.webdriver import ChromeOptions from PIL import Image from selenium.webdriver import ActionChains # 規避檢測 option = ChromeOptions() #例項化一個ChromeOptions物件 option.add_experimental_option('excludeSwitches', ['enable-automation']) #以鍵值對的形式加入引數 bro = webdriver.Chrome(executable_path = './chromedriver',options = option) bro.get('https://kyfw.12306.cn/otn/login/init') bro.maximize_window() sleep(10) print("***************************************") bro.save_screenshot('./123.png') code_img = bro.find_element_by_xpath('/html/body/div[6]/div/form/div/ul[2]/li[4]/div/div/div[3]/img') location = code_img.location size = code_img.size # print(size) # print(location) sleep(10) userName = input('請輸入12306使用者名稱:') passWord = input('請輸入12306密碼 :') # 驗證碼左上角和右下角座標 # rangle = ( # int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) # i = Image.open('123.png') # code_img_name = 'code.png' # frame = i.crop(rangle) # frame.save(code_img_name) # https://kyfw.12306.cn/otn/login/init # 還是有問題,我猜測是獲取驗證碼那裡出的問題 # 直接截圖驗證碼 bro.find_element_by_xpath('/html/body/div[6]/div/form/div/ul[2]/li[4]/div/div/div[3]/img').screenshot('./code.png') chaojiying = Chaojiying_Client('hhh', 'hhh', 'hhh') #使用者中心>>軟體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 = [] 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) for xy in all_list: x = xy[0] y = xy[1] ActionChains(bro).move_to_element_with_offset(code_img,x,y).click().perform() sleep(0.5) bro.find_element_by_id('username').send_keys(userName) bro.find_element_by_id('password').send_keys(passWord) bro.find_element_by_id('loginSub').click() sleep(5)
老是顯示驗證碼校驗錯誤,我感覺應該不是我的問題