在家無(wú)聊,線代和高數(shù)看不懂,整點(diǎn)事情干,就準(zhǔn)備預(yù)定回學(xué)校的高鐵票,于是就有了這個(gè)文章
1.pip安裝chromediver,當(dāng)然也可以手動(dòng)解壓(網(wǎng)上的教程好像沒(méi)有提到pip,手動(dòng)安裝到C盤pycharm里面的Scripts就行了)
chromedriver.storage.googleapis.com/index.html這是chromedriver文件官網(wǎng),在chrome里面設(shè)置查看自己的版本,然后找對(duì)應(yīng)的版本就完了
2.注冊(cè)個(gè)超級(jí)鷹,http://www.chaojiying.com/contact.html,挺厲害的打碼平臺(tái),微信公眾號(hào)綁定一下賬號(hào)給1000積分,足夠干12306驗(yàn)證碼了
1.選擇chrome打開12306然后切換到賬號(hào)登錄
默認(rèn)是掃碼登錄
F12然后點(diǎn)擊賬號(hào)登錄
3.復(fù)制xPath,/html/body/div[2]/div[2]/ul/li[2]/a
代碼實(shí)現(xiàn)
from selenium.webdriver import Chrome web = Chrome() web.get('https://kyfw.12306.cn/otn/resources/login.html') time.sleep(3) web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
2.下載驗(yàn)證碼(截屏也可以)然后發(fā)送給超級(jí)鷹
超級(jí)鷹官網(wǎng)有個(gè)官方文檔,下載然后pychram打開,其實(shí)就很簡(jiǎn)單,然后把賬號(hào)密碼改成你自己的,
from chaojiying import Chaojiying_Client
驗(yàn)證碼需要時(shí)間加載,所以要sleep(3)就夠了,
3.拿到坐標(biāo)然后模擬點(diǎn)擊
好像這個(gè)官方叫什么偏移量,挺高大上的,說(shuō)白了就是建立一個(gè)坐標(biāo)系,給個(gè)x,y然后點(diǎn)擊就完了,默認(rèn)左上方是原點(diǎn)
for pre_location in location_list: #切割出來(lái)x,y坐標(biāo) location = pre_location.split(',') x = int(location[0]) y = int(location[1]) ActionChains(web).move_to_element_with_offset(img,x,y).click().perform()
4.登錄以后有個(gè)滑動(dòng)驗(yàn)證
現(xiàn)在我還沒(méi)有找到方法控制滑動(dòng)速度,勻速運(yùn)動(dòng),但是12306并沒(méi)有因?yàn)檫@個(gè)驗(yàn)證失敗
ActionChains(web).drag_and_drop_by_offset(button,340,0).perform()
button是那個(gè)滑塊的Xpath,我記得好像是長(zhǎng)度330,340肯定是夠用了,那個(gè)0就是豎y的方向上的滑動(dòng)
剛開始12306圖片和滑動(dòng)驗(yàn)證通過(guò)以后一直說(shuō)驗(yàn)證失敗,百思不得其解,百度發(fā)現(xiàn)是因?yàn)檫@個(gè)
這是正常頁(yè)面下的,也就是我改了以后的,加一個(gè)這個(gè)代碼,欺騙一下
def trick_not_chromedriver(): option = Options() option.add_argument('--disable-blink-features=AutomationControlled') return option
這個(gè)要調(diào)用在前面,靠后一點(diǎn)就不行了
from selenium.webdriver import Chrome import requests,time from hashlib import md5 from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.chrome.options import Options 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: 圖片字節(jié) 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:報(bào)錯(cuò)題目的圖片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() #獲取驗(yàn)證碼 def get_verify_img(web): web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click() time.sleep(5) verify_img = web.find_element_by_xpath('//*[@id="J-loginImg"]') return verify_img #識(shí)別驗(yàn)證碼返回坐標(biāo) def discern_verify_img(verify_img): chaojiying = Chaojiying_Client('超級(jí)鷹賬號(hào)', '密碼', '軟件ID') responce = chaojiying.PostPic(verify_img.screenshot_as_png, 9004) pre_location = responce['pic_str'] location_list = pre_location.split("|") # 把split寫錯(cuò)了,卡了半天 # type_pre_location = type(pre_location) return location_list # return type_pre_location #拿到坐標(biāo)模擬點(diǎn)擊 def click_and_enter(web,location_list,img): for pre_location in location_list: #切割出來(lái)x,y坐標(biāo) location = pre_location.split(',') x = int(location[0]) y = int(location[1]) ActionChains(web).move_to_element_with_offset(img,x,y).click().perform() def enter(web): # input() web.find_element_by_xpath('//*[@id="J-userName"]').send_keys('賬號(hào)') web.find_element_by_xpath('//*[@id="J-password"]').send_keys('密碼') web.find_element_by_xpath('//*[@id="J-login"]').click() #滑動(dòng)驗(yàn)證 def move_verify(web): button = web.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span') ActionChains(web).drag_and_drop_by_offset(button,340,0).perform() # 騙12306這不是chromedriver def trick_not_chromedriver(): option = Options() option.add_argument('--disable-blink-features=AutomationControlled') return option #現(xiàn)在有一個(gè)疫情防控的確認(rèn)按鈕,點(diǎn)一下這個(gè) def yqfk(web): web.get('https://kyfw.12306.cn/otn/leftTicket/init') time.sleep(1) web.find_element_by_xpath('//*[@id="qd_closeDefaultWarningWindowDialog_id"]').click() #進(jìn)入查詢界面,思路正則表達(dá)式,不可信 def get_stick_text(web): web.get('https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2021-04-16leftTicketDTO.from_station=TNVleftTicketDTO.to_station=CZFpurpose_codes=0X00') response = web.find_element_by_xpath('/html/body/pre').text return (response) #父子節(jié)點(diǎn)一個(gè)一個(gè)找,顯示余票 if __name__ == '__main__': web = Chrome(options=trick_not_chromedriver()) web.get('https://kyfw.12306.cn/otn/resources/login.html') time.sleep(5) # click_and_enter(discern_verify_img(get_verify_img())) img = get_verify_img(web) click_and_enter(web,discern_verify_img(img),img) time.sleep(5) enter(web) time.sleep(5) move_verify(web) time.sleep(1) yqfk(web) time.sleep(2) get_verify_img(web)
已經(jīng)可以登錄的,結(jié)果就是這個(gè)界面
還有一個(gè)想法是余票檢測(cè),在搞了,應(yīng)該快了
到此這篇關(guān)于python解決12306登錄驗(yàn)證碼的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python 12306登錄驗(yàn)證碼 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:陽(yáng)泉 克拉瑪依 雙鴨山 金華 日照 赤峰 臨汾 貴州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python解決12306登錄驗(yàn)證碼的實(shí)現(xiàn)》,本文關(guān)鍵詞 python,解決,12306,登錄,驗(yàn)證,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。