欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python学习教程,12306火车票抢票系统

程序员文章站 2022-04-15 13:59:56
python学习教程,12306火车票抢票系统 代码展示: 现在Python的发展趋势已经很明显了,以后就是人工智能和大数据的时代,你还不会系统的Python,你能跟上市场发展的需求和步伐嘛,想学习就业或转行的你还在考虑什么,难道要等市场饱和之后你再学习?想学习的你就给自己一次投资学习的机会,不然怎 ......

python学习教程,12306火车票抢票系统

python学习教程,12306火车票抢票系统

代码展示:

  1 '''
  2 在学习过程中有什么不懂得可以加我的python学习交流扣扣qun,934109170,群里有不错的学习教程、开发工具与电子书籍。
  3 与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容。
  4 '''
  5 import urllib.request as request
  6  
  7     import http.cookiejar as cookiejar
  8  
  9     import re
 10  
 11     import os
 12  
 13     import smtplib
 14  
 15     from email.mime.text import mimetext
 16  
 17     import time
 18  
 19     user = '' #登陆邮箱
 20  
 21     pwd = ''#邮箱密码
 22  
 23     to = [''] #发送的邮箱
 24  
 25     with open('d:\python源码\city.txt','r') as f:
 26  
 27        a = f.read()
 28  
 29     station = re.compile(u'\w+:(.+?):(\w+):\d').findall(a)
 30  
 31     dic1 = {}
 32  
 33     for b in range(0, len(station)):
 34  
 35         dic1[station[b][0]] = station[b][1]
 36  
 37     def gethtml(geturl):
 38  
 39         cj = cookiejar.lwpcookiejar()
 40  
 41         cookiejarsupport = request.httpcookieprocessor(cj)
 42  
 43         opener = request.build_opener(cookiejarsupport,request.httphandler)
 44  
 45         headers = {
 46  
 47             'user-agent':'mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, like gecko) chrome/39.0.2171.99 safari/537.36',
 48  
 49             'host':'www.12306.cn',
 50  
 51             'referer':'http://www.12306.cn/opn/lcxxcx/init'
 52  
 53         }
 54  
 55         request.install_opener(opener)
 56  
 57         req = request.request(url=geturl, headers=headers)
 58  
 59         html = request.urlopen(req).read().decode()
 60  
 61         return html
 62  
 63     def getstation(html):
 64  
 65         fromstation = re.compile(r'from_station_name":"(.+?)","').findall(html)
 66  
 67         tostation = re.compile(r'to_station_name":"(.+?)",').findall(html)
 68  
 69         startime =  re.compile(r'"start_time":"(.+?)"').findall(html)
 70  
 71         arrtime = re.compile(r'arrive_time":"(.+?)"').findall(html)
 72  
 73         lishi =  re.compile(r'"lishi":"(.+?)",').findall(html)
 74  
 75         webbuy = re.compile(r'"canwebbuy":"(.+?)').findall(html)
 76  
 77         startstation = re.compile(r'start_station_name":"(.+?)"').findall(html)
 78  
 79         endstation = re.compile(r'end_station_name":"(.+?)"').findall(html)
 80  
 81         ruanwo = re.compile((r'"rw_num":"(.+?)",')).findall(html)
 82  
 83         ruanzuo = re.compile((r'"rz_num":"(.+?)"')).findall(html)
 84  
 85         yingwo = re.compile(r'"yw_num":"(.+?)"').findall(html)
 86  
 87         ruanzuo = re.compile(r'"rz_num":"(.+?)"').findall(html)
 88  
 89         yingzuo = re.compile(r'"yz_num":"(.+?)"').findall(html)
 90  
 91         wuzuo = re.compile(r'"wz_num":"(.+?)"').findall(html)
 92  
 93         checi = re.compile(r'station_train_code":"(.+?)"').findall(html)
 94  
 95         datanum = re.compile((r'day_difference":"(.+?)"')).findall(html)
 96  
 97         erdengzuo = re.compile(r'ze_num":"(.+?)",').findall(html)
 98  
 99         num = range(0, len(yingwo))
100  
101         for i in num:
102  
103             try:
104  
105                 if int(yingzuo[i]) != 0 or int(erdengzuo[i]) != 0 or int(wuzuo[i] !=0):     #z108
106  
107                     print(checi[i], '    二等座:', erdengzuo[i], '    硬座:', yingzuo[i],'   无座:',wuzuo[i])
108  
109                     if yingwo[i] != '--' or yingzuo[i] != '无':
110  
111                         msg=mimetext('火车:'+fromstation[i]+' ->'+tostation[i] +'('+ checi[i]+ ')\n二等座:'+erdengzuo[i]+ '张;硬座:'+ yingzuo[i]+'张;无座:'+wuzuo[i]+ '张!快买去!\n网址:http://www.12306.cn/opn/lcxxcx/init')
112  
113                         msg['subject'] = '有票啦!'
114  
115                         msg['from'] = user
116  
117                         msg['to'] = ','.join(to)
118  
119                         s = smtplib.smtp('smtp.qq.com', timeout = 30) #连接smtp端口
120  
121                         s.login(user,pwd)#登陆服务器
122  
123                         s.sendmail(user,to,msg.as_string())
124  
125                         s.close()
126  
127                         print('发送成功')
128  
129                         print('------------------------------------------------------------')
130  
131             except:
132  
133                 continue
134  
135     print('''''
136 
137 by:王小涛_同學 
138 -------------------------------------------------------------- 
139     欢迎使用! 
140 -------------------------------------------------------------- 
141 ''')  
142 print ('请输入购票类型:(0为成人票   其他为学生票) ')  
143 leixing = input()  
144 print('请输入起点:')  
145 qidian = input()  
146 try:  
147     if dic1[qidian]:  
148         qidian = dic1[qidian]  
149 except:  
150     print('起点输入有误!')  
151 print('请输入终点:')  
152 zhongdian = input()  
153 try:  
154     if dic1[zhongdian]:  
155         zhongdian = dic1[zhongdian]  
156 except:  
157     print('终点输入有误!')  
158 print('请输入购票年份:')  
159 year = input()+'-'  
160 print('请输入购票月份:(2位)')  
161 month = input()+'-'  
162 print('请输入购票日期:(2位)')  
163 date = input()  
164 date = year + month + date  
165  
166  
167 if leixing == 0:  
168    geturl = 'http://www.12306.cn/opn/lcxxcx/query?purpose_codes=adult&querydate='+date+'&from_station='+qidian+'&to_station='+ zhongdian  
169 else:  
170     geturl = 'http://www.12306.cn/opn/lcxxcx/query?purpose_codes=0x00&querydate='+date+'&from_station='+qidian+'&to_station='+ zhongdian  
171 while 1:  
172     getstation(gethtml(geturl))  
173     print('火车票监测中...')  
174     time.sleep(300)  </pre>

现在python的发展趋势已经很明显了,以后就是人工智能和大数据的时代,你还不会系统的python,你能跟上市场发展的需求和步伐嘛,想学习就业或转行的你还在考虑什么,难道要等市场饱和之后你再学习?想学习的你就给自己一次投资学习的机会,不然怎么去改变自己,成为更优秀的自己。学习python爬虫过程中有不懂的可以给我留言,可以关注一下我,我会不定时分享关于python相关小案例及学习经验