python學習教程,12306火車票搶票系統 代碼展示: 現在Python的發展趨勢已經很明顯了,以後就是人工智慧和大數據的時代,你還不會系統的Python,你能跟上市場發展的需求和步伐嘛,想學習就業或轉行的你還在考慮什麼,難道要等市場飽和之後你再學習?想學習的你就給自己一次投資學習的機會,不然怎 ...
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相關小案例及學習經驗