当前位置: 首页 > 后端技术 > Python

Python自动刷新火车票

时间:2023-03-26 16:45:59 Python

!/usr/bin/envpython-*-coding:utf-8-*-"""通过splinter刷火车票,可以自动填写账号密码,同时登录时,还可以修改账号密码然后手动识别验证码,登录,接下来的事情交给脚本,静静等待抢票结果(刷票过程中不能关闭浏览器)"""importrefromsplinter.browserimportBrowserfromtimeimportsleepimporttimeimportsysimporthttplib2fromurllibimportparseimportsmtplibimportnumpyasnpfromemail.mime.textimportMIMETextclassBrushTicket(object):"""购票类及实现方法"""def__init__(self,user_name,password,passengers,from_time,from_station,to_station,number,Seat_type):"定义实例属性,初始化""#1206账号密码seld.user_name=user_nameself.password=password#乘客姓名spassengers#起始站.to_Station=To_Station#Sel.from_time=From_time#....=Number.capitalize()#座位在td位置ifseat_type=="seat_type_index=1ue=9elifseat_type=='一一':seat_type_index=2seat_type_value='m'elifseat_type=='二二二':seat_type_index=3seat_type_value=0elifseat__value=0elifseat_ty_ty_ty_ty__tece_eseat__seat__seate_teex=4teex='':tepe':=6????????elifseat_type=='软卧':????????????seat_type_index=5????????????seat_type_value=4????????elifseat_type=='动卧':????????????seat_type_index=6????????????seat_type_value='F'????????elifseat_type=='硬卧':????????????seat_type_index=7????????????seat_type_value=3????????elifseat_type=='软座':????????????seat_type_index=8????????????seat_type_value=2????????elifseat_type=='硬座':????????????seat_type_index=9????????????seat_type_value=1????????elifseat_type=='无座':????????????seat_type_index=10????????????seat_type_value=1????????elifseat_type=='其他':????????????seat_type_index=11seat_type_value=1其他:seat_type_index=7seat_type_value=3self.seat_type_index=seat_type_indexself.seat_type_value=seat_type_value#/kyfw.12306.cn/otn/vie...'self.ticket_url='https://kyfw.12306.cn/otn/lef...'#浏览器驱动信息,驱动下载页面:https://sites.google.com/a/ch...self.driver_name='chrome'self.executable_path=r'H:\Forpersonaluse\PYTHON\file\chromedriver.exe'defdo_login(self):"""登录函数实现,手动识别验证码登录"""self.driver.visit(self.login_url)sleep(1)self.driver.fill('loginUserDTO.user_name',self.user_name)self.driver.fill('userDTO.password',self.password)print('请输入验证码...)whiletrue:ifself.driver.url!=self.init_my_url:sleep(1):breakdefstart_brush(seld):"买票功能实现"""self.driver=Browser(driver_name=self.driver_name,executable_path=self.executable_path)#浏览器窗口的大小self.driver.driver.set_window_size(1000,900)self.do_login()self.。self.to_station})self.driver.cookies.add({"_jc_save_fromDate":self.from_time})self.driver.reload()count=0whileself.driver.url.split('?')[0]==self.ticket_url:self.driver.find_by_text('查询').click(1)sleep(1)count+=1打印('第%d次次查询查询……'%count)尝试:car_no_location=self.driver。find_by_id("queryLeftTable")[0].find_by_text(self.number)[0]current_tr=car_no_location.find_by_xpath("./../../../../..")如果current_tr.find_by_tag('td')[self.seat_type_index].text=='--':print('no此座位类型出售,当前售票已结束,请重新开启!')sys.exit(1)elifcurrent_tr.find_by_tag('td')[self.seat_type_index].text=='None':,print(Continuetotry...')elifCurrent_tr.find_by_tag('TD')[Self.Seat_Type_INDEX].text=='计算':print('备选,继续尝试...')else:#Trytobookprint('刷票(剩余票数:'+str(current_tr.find_by_tag('td')[self.seat_type_index].text)+'),开始尝试订票...')a')[0].CLICK(1)print('StartSelectUser...)Key_value=1forpinSelf.paSsengers:#SelectuserSELf.driver.find_by_text(p).last.click()#选择座位类型function(){//外汇术语解释http://www.fx61.com/definitionsideatbyseat_Typeseat_select=self.driver.f("str(key_value)[0]seat_select.find_by_xpath("//option[@value='"+str(self.seat_type_value)+"']")[0].click()key_value+=1空格(0.5)(0.5)(0.5)ifp[-1]==')':self.driver.find_by_id('dialog_xSertcj_ok').点击()打印('凌令...')seld.driver.find_by_id('Submitorder_id').click()SLEEP(0.5)#查看结果是否正常_false_info)self.driver.find_by_id('qr_closetranfordialog_id')。click(sleep()sleep(0.2)self.driver.find_by_id('prestep_id')。click('prestep_id')。click(click()sleep()睡眠(0.3).driver.find_by_id('Submitorder_id')。CLICK()sleep(1)print('预订成功,请及时去支付...)#发送内容defmail_to(nerib,biaoti,geishei):text=neireongmimetestxtxtxtxt(Text,'plain','utf-8')msg??['subject']=biaotimsg["from"]=Geisheis=SMTPLib.smtp(SMTPSERVER,25)S.Login(Sender,Passwd)S.Sendmail(sender,sender,msg.as_string())s.quit()mail_to(neirong,biaoti,geishei)#self.driver.quit()extecepioneform_info:print(error_info)#self.driver.quit.quit()print(error_info)self.driver.quit()sys.exit(1)if__name__=='__main__':#城市cookie字典city_list={'bj':'%u5317%u4EAC%2CBJP',#北京'hd':'%u5929%u6D25%2CTJP',#邯郸'nn':'%u5357%u5B81%2CNNZ',#南宁'wh':'%u6B66%u6C49%2CWHN',#武汉'cs':'%u957F%u6C99%2CCSQ',#长沙'ty':'%u592A%u539F%2CTYV',#太原'yc':'%u8FD0%u57CE%2CYNV',#运城'gz':'%u5E7F%u5DDE%2CGZQ',#广州'qhc':'%u6E05%u6CB3%u57CE%2CQYP'#清河市}#邮件信息SMTPServer="smtp.163.com"sender="**********@163.com"passwd="********"c=time.time()b=time.localtime(c)q=time.strftime("%Y-%m-%d%X",b)neirong=("12306:"+q+"拿到票了,我们换乘时间吧!")biaoti=("AletterLetter"+q)geishei=sender#从txt中获取信息withopen(r'H:\Forpersonaluse\PYTHON\file\tickets.txt','r',encoding='utf-8',errors='忽略')asf:info_array=np.genfromtxt(f,dtype=str,delimiter=':')account=info_array[0][1]password=info_array[1][1]from_time=info_array[2][1]]start=info_array[3][1]end=info_array[4][1]from_station=city_list[start]to_station=city_list[end]engnumber=info_array[5][1]1assarray[6][=info_array[7][1].split(",")#打印购票者信息print(account,password,passengers,from_time,from_station,to_station,number,seat_type)#开始抢票ticket=BrushTicket(account,password,passengers,from_time,from_station,to_station,number,seat_type)ticket.start_brush()