python解決12306登錄驗(yàn)證碼的實(shí)現(xiàn)
在家無(wú)聊,線代和高數(shù)看不懂,整點(diǎn)事情干,就準(zhǔn)備預(yù)定回學(xué)校的高鐵票,于是就有了這個(gè)文章
準(zhǔn)備工作1.pip安裝chromediver,當(dāng)然也可以手動(dòng)解壓(網(wǎng)上的教程好像沒(méi)有提到pip,手動(dòng)安裝到C盤(pán)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)證碼了
開(kāi)始實(shí)戰(zhàn)講解1.選擇chrome打開(kāi)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 Chromeweb = 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打開(kāi),其實(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靠webdriver判斷是不是爬蟲(chóng)剛開(kāi)始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 Chromeimport requests,timefrom hashlib import md5from selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.chrome.options import Optionsclass Chaojiying_Client(object): def __init__(self, username, password, soft_id):self.username = usernamepassword = password.encode(’utf8’)self.password = md5(password).hexdigest()self.soft_id = soft_idself.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: 題目類(lèi)型 參考 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寫(xiě)錯(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這不是chromedriverdef 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)入查詢(xún)界面,思路正則表達(dá)式,不可信def get_stick_text(web): web.get(’https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2021-04-16&leftTicketDTO.from_station=TNV&leftTicketDTO.to_station=CZF&purpose_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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子2. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐3. XML入門(mén)的常見(jiàn)問(wèn)題(一)4. jsp EL表達(dá)式詳解5. 解決ajax的delete、put方法接收不到參數(shù)的問(wèn)題方法6. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)7. idea修改背景顏色樣式的方法8. chat.asp聊天程序的編寫(xiě)方法9. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
