import urllib.request as request

import http.cookiejar as cookiejar

import re

import os

import smtplib

from email.mime.text import MIMEText

import time

user = '' #登陆邮箱

pwd = ''#邮箱密码

to = [''] #发送的邮箱

with open('D:\\Python源码\\city.txt','r') as f:

a = f.read()

station = re.compile(u'\w+:(.+?):(\w+):\d').findall(a)

dic1 = {}

for b in range(0, len(station)):

dic1[station[b][0]] = station[b][1]

def gethtml(geturl):

cj = cookiejar.LWPCookieJar()

cookiejarsupport = request.HTTPCookieProcessor(cj)

opener = request.build_opener(cookiejarsupport,request.HTTPHandler)

headers = {

'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36',

'Host':'www.12306.cn',

'Referer':'http://www.12306.cn/opn/lcxxcx/init'

}

request.install_opener(opener)

req = request.Request(url=geturl, headers=headers)

html = request.urlopen(req).read().decode()

return html

def getstation(html):

fromstation = re.compile(r'from_station_name":"(.+?)","').findall(html)

tostation = re.compile(r'to_station_name":"(.+?)",').findall(html)

startime = re.compile(r'"start_time":"(.+?)"').findall(html)

arrtime = re.compile(r'arrive_time":"(.+?)"').findall(html)

lishi = re.compile(r'"lishi":"(.+?)",').findall(html)

webbuy = re.compile(r'"canWebBuy":"(.+?)').findall(html)

startstation = re.compile(r'start_station_name":"(.+?)"').findall(html)

endstation = re.compile(r'end_station_name":"(.+?)"').findall(html)

ruanwo = re.compile((r'"rw_num":"(.+?)",')).findall(html)

ruanzuo = re.compile((r'"rz_num":"(.+?)"')).findall(html)

yingwo = re.compile(r'"yw_num":"(.+?)"').findall(html)

ruanzuo = re.compile(r'"rz_num":"(.+?)"').findall(html)

yingzuo = re.compile(r'"yz_num":"(.+?)"').findall(html)

wuzuo = re.compile(r'"wz_num":"(.+?)"').findall(html)

checi = re.compile(r'station_train_code":"(.+?)"').findall(html)

datanum = re.compile((r'day_difference":"(.+?)"')).findall(html)

erdengzuo = re.compile(r'ze_num":"(.+?)",').findall(html)

num = range(0, len(yingwo))

for i in num:

try:

if int(yingzuo[i]) != 0 or int(erdengzuo[i]) != 0 or int(wuzuo[i] !=0): #Z108

print(checi[i], ' 二等座: ', erdengzuo[i], ' 硬座: ', yingzuo[i],' 无座: ',wuzuo[i])

if yingwo[i] != '--' or yingzuo[i] != '无':

msg=MIMEText('火车: '+fromstation[i]+' ->'+tostation[i] +'('+ checi[i]+ ')\n二等座:'+erdengzuo[i]+ '张;硬座:'+ yingzuo[i]+'张;无座:'+wuzuo[i]+ '张!快买去!\n网址: http://www.12306.cn/opn/lcxxcx/init')

msg['Subject'] = '有票啦!'

msg['From'] = user

msg['To'] = ','.join(to)

s = smtplib.SMTP('smtp.qq.com', timeout = 30) #连接SMTP端口

s.login(user,pwd)#登陆服务器

s.sendmail(user,to,msg.as_string())

s.close()

print('发送成功')

print('------------------------------------------------------------')

except:

continue

print('''''

By:王小涛_同學

--------------------------------------------------------------

欢迎使用!

--------------------------------------------------------------

''')

print ('请输入购票类型: (0为成人票 其他为学生票) ')

leixing = input()

print('请输入起点:')

qidian = input()

try:

if dic1[qidian]:

qidian = dic1[qidian]

except:

print('起点输入有误!')

print('请输入终点:')

zhongdian = input()

try:

if dic1[zhongdian]:

zhongdian = dic1[zhongdian]

except:

print('终点输入有误!')

print('请输入购票年份:')

year = input()+'-'

print('请输入购票月份:(2位)')

month = input()+'-'

print('请输入购票日期:(2位)')

date = input()

date = year + month + date

if leixing == 0:

geturl = 'http://www.12306.cn/opn/lcxxcx/query?purpose_codes=ADULT&queryDate='+date+'&from_station='+qidian+'&to_station='+ zhongdian

else:

geturl = 'http://www.12306.cn/opn/lcxxcx/query?purpose_codes=0X00&queryDate='+date+'&from_station='+qidian+'&to_station='+ zhongdian

while 1:

getstation(gethtml(geturl))

print('火车票监测中...')

time.sleep(300)

判断火车票座位python代码_12306 火车票监控Python代码详解相关推荐

  1. python小游戏代码大全-20行python代码的入门级小游戏的详解

    背景: 作为一个python小白,今天从菜鸟教程上看了一些python的教程,看到了python的一些语法,对比起来(有其他语言功底),感觉还是非常有趣,就随手添了一点内容,改了一个小例程,当着练练手 ...

  2. python中返回上一步操作的代码_Pycharm代码跳转后退回操作详解

    用Pycharm写Python代码有一段时间了,最近发现了一个Pycharm的一个小技巧想分享给大家,下面这篇文章主要给大家介绍了关于Pycharm代码跳转该如何回退的相关资料,文中介绍的非常详细,对 ...

  3. 使用python代码给手机发短信详解(twilio的使用)

    ☞☞☞点击查看更多优秀Python博客☜☜☜ 使用python代码给手机发短信详解 twilio网页注册 实现代码 效果演示 ==**文章导航:==** 使用python代码给手机发送短信,在这里给大 ...

  4. Python中的__name__和__main__含义详解

    背景 在写Python代码和看Python代码时,我们常常可以看到这样的代码: ? 1 2 3 4 5 def main():     ...... if __name__ == "__ma ...

  5. python 3.x 爬虫基础---http headers详解

    python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---Requer ...

  6. python中验证码连通域分割的方法详解

    python中验证码连通域分割的方法详解 这篇文章主要给大家介绍了关于python中验证码连通域分割的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需 ...

  7. Zabbix监控Jenkins过程详解

    这篇文章主要介绍了基于zabbix实现监控Jenkins的过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值. 一.监控架构图 二.实现思路 在 Jenkins 上安装 ...

  8. python菜单怎么做_Python 城市菜单详解(超详解)

    print("--------城市查询系统---------") print("--------按数值进行查询--------") menu={"内蒙 ...

  9. Linux系统与网络、磁盘参数和日志监控等命令详解二

    创作人QQ:851301776,邮箱:lfr890207@163.com, 欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点! 个人座右铭: 1.没有横空出世,只要厚积一定发. ...

  10. python实现连续变量最优分箱详解--CART算法

    今天小编就为大家分享一篇python实现连续变量最优分箱详解–CART算法,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 关于变量分箱主要分为两大类:有监督型和无监督型 对应的分箱方 ...

最新文章

  1. Asp.net后台创建HTML
  2. linux qt creator git,Building Qt Creator from Git/zh
  3. python数据预测模型算法_如何对时间序列预测问题计算基准预测(python)
  4. Matlab简单系统仿真示例1
  5. ubuntu raid 配置磁盘阵列
  6. C++和Java中类继承同名函数的区分
  7. 调整xfce中的鼠标大小到48以上-目前还没有完成
  8. Defensive programming -- Code complete reading notes(8)
  9. 设计模式--6大原则--单一职责原则
  10. 洛谷 P3382 【模板】三分法
  11. 浅谈tcpwrapper的基本使用方法
  12. typescript 怎么表示当前时间减一个月_吃什么减肚子最快最有效 4种刮油食物吃出小蛮腰...
  13. Redis实现邮件激活码保存
  14. 一张帖搞定同学们入学黑马前所有难题
  15. LGTM? 那些迷之缩写
  16. 内网环境下如何配置CentOS网络源(阿里云)----nginx代理实现
  17. java word转图片(word转pdf再转图片)
  18. 遥感或DEM像素深度如何降为8bit
  19. 《SolidWorks 2014中文版完全自学手册》——导读
  20. EasyExcel导出添加水印

热门文章

  1. matlab 非支配排序,NSGA-II快速非支配排序算法理解
  2. python灰产路子有哪些_Python3 网络爬虫(四):视频下载,那些事儿!-后台/架构/数据库-敏捷大拇指-一个敢保留真话的IT精英社区...
  3. 网页性能优化之图片懒加载
  4. 【具有独到技术的软件卸载工具】
  5. 服务器假死 http 状态码,关于golang:Golang之HTTP服务『假死』问题排查
  6. 天下数据解析域名及域名转向
  7. oracle数据库HugePages配置
  8. 安卓图片三级缓存策略与实现
  9. 优秀工程师应该具备哪些素质_工程师应具备的素养有哪些
  10. ie无法下载 无法打开该internet站点.请求的站点不可用或无法找到.请稍后重试