Python实现12306火车票抢票系统
作者:听雪楼萧忆情 发布时间:2023-09-30 01:33:31
标签:python,12306,抢票
Python实现12306火车票抢票系统效果图如下所示:
具体代码如下所示:
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) </pre>
总结
以上所述是小编给大家介绍的Python实现12306火车票抢票系统,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/xiaoyiq/archive/2019/07/04/11130297.html


猜你喜欢
- JavaScript图片水平翻转后垂直翻反转的特效一:<!--把下列代码加到body区域内--><SCRIPT langu
- 返回函数所谓返回函数,顾名思义,就是把函数作为返回值。高阶函数除了可以将函数作为参数之外,还可以将函数作为结果进行返回。下面来实现一个可变参
- 1.首先,打开python的官网:python.org2.首页downloads下打开,3.最上边是两个最新的版本,长期计划,推荐使用pyt
- 1、表中字段区分大小写的设置在使用gorm查询的时候,会出现账户名A和a是一样的情况,是因为mysql默认不区分大小写造成的1.问题产生的原
- 项目中经常会遇到这样的问题:当某个 js 脚本加载完成后再执行相应任务,但很多朋友可能并不知道怎么判断我们要加载的 js 文件是否加载完成,
- 什么是pyc文件pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,加载的速度有
- 使用.net2005自带的SQL-Express连接不上。解决方法:1.网络防火墙阻止数据库连接;2.默认SQL-Express没有启动Sa
- 目录原生 JS怎么发送一个 get 请求怎么发送一个 post 请求发送一个带有参数的 get 请求发送一个带有参数的 post 请求jQu
- 1.线性与非线性回归线性回归 Linear Regression:两个变量之间的关系是一次函数关系的—&mdas
- 如下所示:matplotlib subplots 设置总图的标题 :fig.suptitle(dname,fontsize=16,x=0.5
- 当需要从 JavaScript 中的数组中获取最后一个元素时,有多种选择,本文将提供 3 种可用方法。1. 数组 length 属性??le
- 前言在使用echarts绘图时可能遇到一些特别的需求,如,当x周表示日期且数据较多(如三个月,按天统计),此时如果按照echarts的默认配
- 前言:『入门MySQL』系列文章已经完结,今后我的文章还是会以MySQL为主,主要记录下近期工作及学习遇到的场景或者自己的感悟想法,可能后续
- 本文实例讲述了JS实现获取毫秒值及转换成年月日时分秒的方法。分享给大家供大家参考,具体如下:时间日期对象创建方式一var date=new
- 如何用ASP来识别操作系统是vista的?我在网上找了个函数,但是不能判断是vista系统,希望大家帮忙. 这个是我在网上找的函数: Fun
- 一、使用logging.config.dictConfig()函数读取配置信息,参数是字典类型with open(file="./
- 首先需要安装Win32-ODBC模块,具体的步骤如下:1:从TOOLS栏目中下载Win32-ODBC.zip,下载完后用winzip解开到一
- 场景说明假设有一个mysql表被水平切分,分散到多个host中,每个host拥有n个切分表。 如果需要并发去访问这些表,快速得到查询结果,
- 首先,我当时出现的问题是newproject创建的时候没有django的选项,查了半天发现我安装的pycharm是社区版本。所以需要用终端命
- 由于python多线程无法发挥多核的作用,因此当计算量很大的时候就需要考虑多进程。只不过多进程比较麻烦一些,进程中通信向来是一件麻烦事。py