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

2019-07-24 09:13:20来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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相关小案例及学习经验


原文链接:https://www.cnblogs.com/xiaoyiq/p/11130297.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python 之 面向对象基础(继承与派生,经典类与新式类)

下一篇:python格式化输出