python学习教程,12306火车票抢票系统
	  代码展示:
	  1'''
	  2在学习过程中有什么不懂得可以加我的python学习交流扣扣qun,934109170,群里有不错的学习教程、开发工具与电子书籍。
	  3与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容。
	  4'''
	  5importurllib.requestasrequest
	  6
	  7importhttp.cookiejarascookiejar
	  8
	  9importre
	  10
	  11importos
	  12
	  13importsmtplib
	  14
	  15fromemail.mime.textimportMIMEText
	  16
	  17importtime
	  18
	  19user=''#登陆邮箱
	  20
	  21pwd=''#邮箱密码
	  22
	  23to=['']#发送的邮箱
	  24
	  25withopen('D:\Python源码\city.txt','r')asf:
	  26
	  27a=f.read()
	  28
	  29station=re.compile(u'\w+:(.+?):(\w+):\d').findall(a)
	  30
	  31dic1={}
	  32
	  33forbinrange(0,len(station)):
	  34
	  35dic1[station[b][0]]=station[b][1]
	  36
	  37defgethtml(geturl):
	  38
	  39cj=cookiejar.LWPCookieJar()
	  40
	  41cookiejarsupport=request.HTTPCookieProcessor(cj)
	  42
	  43opener=request.build_opener(cookiejarsupport,request.HTTPHandler)
	  44
	  45headers={
	  46
	  47'User-Agent':'Mozilla/5.0(WindowsNT6.3;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/39.0.2171.99Safari/537.36',
	  48
	  49'Host':'www.12306.cn',
	  50
	  51'Referer':'http://www.12306.cn/opn/lcxxcx/init'
	  52
	  53}
	  54
	  55request.install_opener(opener)
	  56
	  57req=request.Request(url=geturl,headers=headers)
	  58
	  59html=request.urlopen(req).read().decode()
	  60
	  61returnhtml
	  62
	  63defgetstation(html):
	  64
	  65fromstation=re.compile(r'from_station_name":"(.+?)","').findall(html)
	  66
	  67tostation=re.compile(r'to_station_name":"(.+?)",').findall(html)
	  68
	  69startime=re.compile(r'"start_time":"(.+?)"').findall(html)
	  70
	  71arrtime=re.compile(r'arrive_time":"(.+?)"').findall(html)
	  72
	  73lishi=re.compile(r'"lishi":"(.+?)",').findall(html)
	  74
	  75webbuy=re.compile(r'"canWebBuy":"(.+?)').findall(html)
	  76
	  77startstation=re.compile(r'start_station_name":"(.+?)"').findall(html)
	  78
	  79endstation=re.compile(r'end_station_name":"(.+?)"').findall(html)
	  80
	  81ruanwo=re.compile((r'"rw_num":"(.+?)",')).findall(html)
	  82
	  83ruanzuo=re.compile((r'"rz_num":"(.+?)"')).findall(html)
	  84
	  85yingwo=re.compile(r'"yw_num":"(.+?)"').findall(html)
	  86
	  87ruanzuo=re.compile(r'"rz_num":"(.+?)"').findall(html)
	  88
	  89yingzuo=re.compile(r'"yz_num":"(.+?)"').findall(html)
	  90
	  91wuzuo=re.compile(r'"wz_num":"(.+?)"').findall(html)
	  92
	  93checi=re.compile(r'station_train_code":"(.+?)"').findall(html)
	  94
	  95datanum=re.compile((r'day_difference":"(.+?)"')).findall(html)
	  96
	  97erdengzuo=re.compile(r'ze_num":"(.+?)",').findall(html)
	  98
	  99num=range(0,len(yingwo))
	  100
	  101foriinnum:
	  102
	  103try:
	  104
	  105ifint(yingzuo[i])!=0orint(erdengzuo[i])!=0orint(wuzuo[i]!=0):#Z108
	  106
	  107print(checi[i],'二等座:',erdengzuo[i],'硬座:',yingzuo[i],'无座:',wuzuo[i])
	  108
	  109ifyingwo[i]!='--'oryingzuo[i]!='无':
	  110
	  111msg=MIMEText('火车:'+fromstation[i]+'->'+tostation[i]+'('+checi[i]+')\n二等座:'+erdengzuo[i]+'张;硬座:'+yingzuo[i]+'张;无座:'+wuzuo[i]+'张!快买去!\n网址:http://www.12306.cn/opn/lcxxcx/init')
	  112
	  113msg['Subject']='有票啦!'
	  114
	  115msg['From']=user
	  116
	  117msg['To']=','.join(to)
	  118
	  119s=smtplib.SMTP('smtp.qq.com',timeout=30)#连接SMTP端口
	  120
	  121s.login(user,pwd)#登陆服务器
	  122
	  123s.sendmail(user,to,msg.as_string())
	  124
	  125s.close()
	  126
	  127print('发送成功')
	  128
	  129print('------------------------------------------------------------')
	  130
	  131except:
	  132
	  133continue
	  134
	  135print('''''
	  136
	  137By:王小涛_同學
	  138--------------------------------------------------------------
	  139欢迎使用!
	  140--------------------------------------------------------------
	  141''')
	  142print('请输入购票类型:(0为成人票其他为学生票)')
	  143leixing=input()
	  144print('请输入起点:')
	  145qidian=input()
	  146try:
	  147ifdic1[qidian]:
	  148qidian=dic1[qidian]
	  149except:
	  150print('起点输入有误!')
	  151print('请输入终点:')
	  152zhongdian=input()
	  153try:
	  154ifdic1[zhongdian]:
	  155zhongdian=dic1[zhongdian]
	  156except:
	  157print('终点输入有误!')
	  158print('请输入购票年份:')
	  159year=input()+'-'
	  160print('请输入购票月份:(2位)')
	  161month=input()+'-'
	  162print('请输入购票日期:(2位)')
	  163date=input()
	  164date=year+month+date
	  165
	  166
	  167ifleixing==0:
	  168geturl='http://www.12306.cn/opn/lcxxcx/query?purpose_codes=ADULT&queryDate='+date+'&from_station='+qidian+'&to_station='+zhongdian
	  169else:
	  170geturl='http://www.12306.cn/opn/lcxxcx/query?purpose_codes=0X00&queryDate='+date+'&from_station='+qidian+'&to_station='+zhongdian
	  171while1:
	  172getstation(gethtml(geturl))
	  173print('火车票监测中...')
	  174time.sleep(300)</pre>
	  现在Python的发展趋势已经很明显了,以后就是人工智能和大数据的时代,你还不会系统的Python,你能跟上市场发展的需求和步伐嘛,想学习就业或转行的你还在考虑什么,难道要等市场饱和之后你再学习?想学习的你就给自己一次投资学习的机会,不然怎么去改变自己,成为更优秀的自己。学习python爬虫过程中有不懂的可以给我留言,可以关注一下我,我会不定时分享关于python相关小案例及学习经验
     如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h54708.shtml








