国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python自動(dòng)登錄12306并自動(dòng)點(diǎn)擊驗(yàn)證碼完成登錄的實(shí)現(xiàn)源代碼

2020-01-04 15:22:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

以下代碼可自動(dòng)登錄12306 - 包括輸入用戶名密碼以及自動(dòng)識(shí)別驗(yàn)證碼并點(diǎn)擊驗(yàn)證碼登陸。該源碼需要稍作修改:

把  username.send_keys('xxxxxxx')  中的  xxxxxx 改為 你自己的12306賬號(hào)。

把  password.send_keys('yyyyyy')     中的 yyyyy 改為自己的 12306 密碼。

即可運(yùn)行。

該源碼把自動(dòng)搶票的核心功能:識(shí)別驗(yàn)證碼并點(diǎn)擊驗(yàn)證碼登陸實(shí)現(xiàn)了。

把代碼稍作加工,即可變?yōu)樽约旱淖詣?dòng)搶票代碼。

運(yùn)行環(huán)境 - 需要安裝python/270766.html">python運(yùn)行環(huán)境,selenium,requests,瀏覽器默認(rèn)為chrome。

運(yùn)行時(shí) 程序會(huì)自動(dòng)分析并識(shí)別驗(yàn)證碼并點(diǎn)擊驗(yàn)證碼,完成登陸過(guò)程。。。

詳細(xì)代碼如下:

#12306 自動(dòng)打開(kāi)12306網(wǎng)站,并輸入用戶名、密碼和驗(yàn)證碼,并登錄12306,#author bigluo#email: 3490699170@qq.com#coding=utf-8from selenium import webdriverimport timefrom PIL import Imagefrom selenium.webdriver.common.action_chains import ActionChainsimport osimport requestsimport numpy#指定button id和button文本值,并點(diǎn)擊,連續(xù)點(diǎn)擊5次#return:#0 click successfully#-1 連續(xù)5次均failed#1 txt != dest_text,所以不點(diǎn)擊def click_button(b,id,dest_text,j):  #在當(dāng)前頁(yè)面查找并點(diǎn)擊指定text,錯(cuò)誤返回 -1.連續(xù)5次,錯(cuò)誤時(shí)延時(shí)1秒 txt='' for i in range(0,5):  try:   txt=b.find_element_by_id(id).text   if txt == dest_text:    b.find_element_by_id(id).click()    return 0    else:    return 1  except:   time.sleep(1)   continue return -1       #5次都失敗了#給定button id和text,find a given text#0 found#-1 not founddef find_button(b,id,dest_text): txt='' try:   txt=b.find_element_by_id(id).text   if txt == dest_text:    return 0 except:     #print("find_button Error --page txt is "+txt+" input text is "+dest_text)   return -1 return -1#click refresh pic buttondef click_refresh(b): try:  b.find_element_by_xpath("//*[@id='loginForm']/div/ul[2]/li[4]/div/div/div[1]").click() except:  print("click_refresh:exception!!!!")#初始化瀏覽器 def init_browser(b): b.maximize_window()#進(jìn)入登錄頁(yè),必須是未登錄狀態(tài)# 0 : 成功#-1 : 出錯(cuò)了def visit_login_page(b): url = 'https://kyfw.12306.cn/otn/index/init' b.get(url) if find_button(b,u"login_user",u"登錄") != 0: #沒(méi)退出  click_button(b,u"regist_out",u"退出",0)  #點(diǎn)擊退出  time.sleep(5)         #休息5秒再查看是否退出 if click_button(b,u"login_user",u"登錄",0) != 0: #點(diǎn)擊登陸按鈕  return -1     #Error happened!! time.sleep(10)     #訪問(wèn)login page后休息10秒,等待驗(yàn)證碼圖片加載完成  return 0#截取一張驗(yàn)證碼圖片,保存為aa.pngdef get_a_verify_pic(b): imgelement=b.find_element_by_xpath("//*[@id='loginForm']/div/ul[2]/li[4]/div/div/div[3]") location = imgelement.location #獲取驗(yàn)證碼x,y軸坐標(biāo)  size=imgelement.size #獲取驗(yàn)證碼的長(zhǎng)寬 rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #寫(xiě)成我們需要截取的位置坐標(biāo) b.save_screenshot('aa.png') i=Image.open("aa.png") #打開(kāi)截圖 pic_name='verify_code'+".jpg" #標(biāo)準(zhǔn)12306驗(yàn)證圖片 frame4=i.crop(rangle) #使用Image的crop函數(shù),從截圖中再次截取我們需要的區(qū)域 frame4.save(pic_name) return pic_name#破解圖片驗(yàn)證碼def ana_pic(b,pic_name): body_list=[] url='''http://littlebigluo.qicp.net:47720/''' files={'file':(pic_name,open(pic_name,'rb'),'image/png')} res=requests.post(url,files=files)    #post pic if res.status_code == 200:      #return ok  try:   #print(res.text)   if u"文字應(yīng)該" in res.text:    #識(shí)別驗(yàn)證碼成功        body_str_1=res.text.split(u'''<B>''')    body_str=body_str_1[2].split(u'<')[0].split()       for index in body_str:     body_list.append(int(index))    return 0,numpy.array(body_list)  except:   print("ana pic failed!!!!")   return -1,None return -1,None     #驗(yàn)證碼解析失敗#按輸入的下標(biāo),點(diǎn)擊一張驗(yàn)證碼圖片def click_one_pic(b,i): try:  imgelement=b.find_element_by_xpath("//*[@id='loginForm']/div/ul[2]/li[4]/div/div/div[3]")  if i<=4:   ActionChains(b).move_to_element_with_offset(imgelement,40+72*(i-1),73).click().perform()  else:   i -= 4   ActionChains(b).move_to_element_with_offset(imgelement,40+72*(i-1),145).click().perform() except:  print("Wa -- click one pic except!!!")#按bodylist 指示,點(diǎn)擊指定驗(yàn)證圖片def click_pic(b,body_list): for i in range(len(body_list)):  click_one_pic(b,body_list[i])  time.sleep(1)#輸入用戶名密碼,并點(diǎn)擊驗(yàn)證碼登陸#0:login successfully#1:verify code failed,#-1 error happeneddef login(b): pic_name=None try:  pic_name=get_a_verify_pic(b)   #截取12306驗(yàn)證碼圖片  ret_val,body_list=ana_pic(b,pic_name) #破解12306驗(yàn)證碼  username=b.find_element_by_id('username')  username.clear()  username.send_keys('xxxxxx')  password=b.find_element_by_id('password')  password.clear()  password.send_keys('yyyyyyy')   time.sleep(2)   if ret_val != 0:   #print("login : what??? predict return error!!")   print("login -- no verified pic!!! !!")   os.remove(pic_name)    #exception occured   #click_refresh(b)   return -1  if len(body_list) == 0:    #no pic recognized   click_refresh(b)   print("login : what??? body list is null!!!")   os.remove(pic_name)    #exception occured   return 1      #verified failed  click_pic(b,body_list)  time.sleep(1)      #休息1秒再點(diǎn)擊登陸按鈕  if click_button(b,u"loginSub",u"登錄",0) != 0:   print("login : what??? click button exception!!!")   return -1     #Error happened!!  except:  if None != pic_name:   os.remove(pic_name)    #exception occured  print("login:exception!!")  return -1 time.sleep(5)      #查看驗(yàn)證碼是否正確?? ret_val=find_button(b,u"error_msgmypasscode1",u"請(qǐng)點(diǎn)擊正確的驗(yàn)證碼") if ret_val == 0:     #驗(yàn)證碼錯(cuò)誤  print("login--Verified code error!!!")  return 1 os.remove(pic_name) print("login--successfully!!!") return 0#循環(huán)login#返回#0:登陸成功-正常返回#-1:登陸失敗或異常返回#1 :驗(yàn)證碼未識(shí)別出來(lái)def try_login(b): for k in range(0,5):     #連續(xù)嘗試5次  rt_val=login(b)  if rt_val < 0:      #error happened   print("verify got exception!!")   time.sleep(10)   continue  elif rt_val == 1:     #verified code error   print("verify - code error!!")   time.sleep(5)   continue      #login again  else:        #login successfully   print("login successfully!!!")   return 0     return -1     #login failedif __name__ == "__main__":  b = webdriver.Chrome() init_browser(b) visit_login_page(b) ret_val = try_login(b) #嘗試登錄 if ret_val<0:    print("main -- try_login failed!!!")  else:  print("main -- try_login successfully!!!")  print("Good job!bigluo??!") 

總結(jié)

以上所述是小編給大家介紹的python自動(dòng)登錄12306并自動(dòng)點(diǎn)擊驗(yàn)證碼完成登錄的實(shí)現(xiàn)源代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 霍城县| 高密市| 澄江县| 津南区| 沛县| 唐海县| 隆安县| 博白县| 南木林县| 浮梁县| 罗田县| 冕宁县| 金塔县| 梁河县| 合川市| 北京市| 富川| 蓬安县| 博白县| 贵德县| 和平县| 元氏县| 高州市| 阿尔山市| 思茅市| 杭州市| 罗城| 安达市| 泰安市| 东兰县| 诸城市| 高邮市| 绍兴市| 大安市| 昌图县| 新宁县| 丰原市| 辽源市| 宁武县| 广丰县| 长岛县|