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
0
投稿
猜你喜欢
- 要求:编写登陆接口输入用户名密码认证成功后显示欢迎信息输错三次后锁定针对此实例写了有二种类型的脚本,略有不同,具体如下:帐号文件accoun
- 你一定很喜欢像QQ或者MSN好友登录提示的效果吧,那么怎么样能在网页中实现呢?asp之家为大家找到了这
- 下面是最终代码 (windows下实现的) # -*- coding: cp936 -*- import os path = 'D:
- 在一些特殊的业务场景中,我们需要一次性提取一张图片中的色卡信息,并且需要使用十六进制的颜色表示方法进行展示。今天得空做了一个小工具,用来自定
- Css tranistions允许元素的属性在单位时间内发生平滑的过渡,在阅读完《CSS Transitions Module Level
- 前言 BeautifulSoup是主要以解析web网页的Python模块,它会提供一些强大的解释器,以解
- oracle 的表空间实例详解查询表空间SELECT UPPER(F.TABLESPACE_NAME) "表空间名",
- 大家经常在网上登陆的时候经常会看到让你输入验证码,有的是文字的,有的呢是图片,比如chinaren.com校友录中留言的时候,我们就会看到数
- 新闻系统、blog系统等都可能用到将动态页面生成静态页面的技巧来提高页面的访问速度,从而减轻服务器的压力,本文为大家搜集整理了ASP编程中常
- 本文实例讲述了Python类的用法。分享给大家供大家参考。具体如下:先看一段代码:#!/usr/bin/env pythonclass Te
- 前言动态生成表格是前端开发中非常重要的内容,常常是后端返回数据(大多是json类型),我们前端通过js循环来动态添加,所以这部分内容是十分重
- 前言:本文的主要内容是介绍Python中的列表及其方法的使用,涉及到的方法包括对列表元素进行修改、添加、删除、排序以及求列表长度等,此外还介
- 在良好的数据库设计基础上,能有效地使用索引是SQL Server取得高性能的基础,SQL Server采用基于代价的优化模型,它对每一个提交
- items()方法返回字典的(键,值)元组对的列表语法以下是items()方法的语法:dict.items()参数 &
- 在以前的日志中讲了怎么制作验证码,这篇就讲讲怎么给验证码加上起干扰效果的杂点。 其实很简单,首先做一个
- 阅读之前:在看文章具体内容之前,希望你可以 先打开IE8,打开http://www.taobao.com,然后在地址栏里输入:javascr
- 先下载:http://signup.qq.com/js/a.js用 IntelliJ IDEA 格式化代码:var naa = true;O
- 现在需要将course分组,然后选择出每一组里面的最大值和最小值,并保留下来实现下面数据结果:直接使用groupby函数,不能直接达到此效果
- 本文主要是利用Python的第三方库Pillow,实现单通道灰度图像的颜色翻转功能。# -*- encoding:utf-8 -*-impo
- 本文实例为大家分享了Python Pygame实现俄罗斯方块的具体代码,供大家参考,具体内容如下源码:# coding : utf-8#: