python抢票
⑴ 如何用python写一个简单的12306抢票软件
所谓抢票实际上是在开始放票的一瞬间第一个发出请求并点击预订。作为程序员的我们,完全可以让程序来做这件事。我花了几个小时写了一个demo。用到的工具集有:Python3.6, Selenium, chromdriver. 程序本身就是流程性的东西,没有什么可介绍的。代码如下
#
coding=utf-8
from
selenium import
webdriver
from
time import
sleep
import
traceback
TICKET_URI
= '
https://kyfw.12306.cn/otn/leftTicket/init
'
LOGIN_URI
= '
https://kyfw.12306.cn/otn/login/init
'
MY_URI
= '
https://kyfw.12306.cn/otn/index/initMy12306
'
LOGIN
= u'
登录
'
from
splinter.browser import
Browser
from
time import
sleep
import
traceback
TICKET_URI
= '
https://kyfw.12306.cn/otn/leftTicket/init
'
LOGIN_URI
= '
https://kyfw.12306.cn/otn/login/init
'
MY_URI
= '
https://kyfw.12306.cn/otn/index/initMy12306
'
LOGIN
= '
login_user
'
def
login():
brw.find_element_by_id(LOGIN).click()
sleep(
3)
uname
= '123456789
@qq.com
'
pwd
= 'xxxyyyzzz
'
brw.find_element_by_id(
'
username
'
).send_keys(uname)
sleep(
1)
brw.find_
⑵ 自动化抢票需要刷新网页吗 python
够用就行。东西就是来用的。比如我做web接口测试,我有python基础知识,会用httplib\urllib2等库就行了。其实在使用过程中,你遇到问题需要解决,自然会促使你去学习新东西。
⑶ 如何用python写一个简单的12306抢票软件
#coding=utf-8
from selenium import webdriver
from time import sleep
import traceback
TICKET_URI = 'https://kyfw.12306.cn/otn/leftTicket/init'
LOGIN_URI = 'https://kyfw.12306.cn/otn/login/init'
MY_URI = 'https://kyfw.12306.cn/otn/index/initMy12306'
LOGIN = u'登录'
from splinter.browser import Browser
from time import sleep
import traceback
TICKET_URI = 'https://kyfw.12306.cn/otn/leftTicket/init'
LOGIN_URI = 'https://kyfw.12306.cn/otn/login/init'
MY_URI = 'https://kyfw.12306.cn/otn/index/initMy12306'
LOGIN = 'login_user'
def login():
brw.find_element_by_id(LOGIN).click()
sleep(3)
uname = '[email protected]'
pwd = 'xxxyyyzzz'
brw.find_element_by_id('username').send_keys(uname)
sleep(1)
brw.find_element_by_id('password').send_keys(pwd)
sleep(1)
while True:
if brw.current_url != MY_URI:
sleep(1)
else:
break
def addCookie(cklist):
li = list()
for d in cklist:
if d['name'] == '_jc_save_toStation' or d['name'] == '_jc_save_toDate' or d['name'] == '_jc_save_fromStation':
li.append(d)
return li
def book():
global brw
brw = webdriver.Chrome()
brw.set_window_size(1366, 768)
brw.get(TICKET_URI)
sleep(3)
while brw.find_element_by_id(LOGIN):
login()
if brw.current_url == MY_URI:
break;
try:
brw.get(TICKET_URI)
sleep(2)
# set src
brw.find_element_by_id('fromStationText').clear()
brw.find_element_by_id('fromStationText').click()
brw.find_element_by_id('fromStationText').send_keys(u'合肥南')
sleep(3)
# set dst
brw.find_element_by_id('toStationText').clear()
brw.find_element_by_id('toStationText').click()
brw.find_element_by_id('toStationText').send_keys(u'武汉')
sleep(3)
# set left date
print('please click train date')
sleep(5)
cke = brw.get_cookies()
li = addCookie(cke)
for x in li:
brw.add_cookie(x)
brw.refresh()
count = 0
success = False
if not success:
while brw.current_url == TICKET_URI:
brw.find_element_by_id('query_ticket').click()
sleep(2)
print(u'第%d次刷新' % count)
count += 1
brw.find_element_by_partial_link_text('D3057')
except Exception as e:
print(traceback.print_exc())
if __name__ == "__main__":
book()
⑷ 学了bython真的可以制作抢票软件吗
这个真可以的,C++和java能编写的bython也可以编写,但是你编写的软件能不能抢的过那些买票软件就需要你的服务器运营和编写能力了。以前见过一个叫分流抢票的软件,是一个大神自己编写的,有兴趣可以自己查查看看。他的AI识别做得很好,目前那些乱七八糟的验证码需要AI自动识别。
⑸ 我从github上下载了python12306的抢票程序,但运行时一堆错误,求各位大神帮帮忙哈
贴一下具体链接呢
还有github上的代码,一般star至少要几十个的,才有一定质量保证
⑹ 怎么使用eclisp开发python抢票代码
我复制你的代码运行了一遍,没问题啊。filemunu=wx.Menu()filemenu.Append(wx.ID_ABOUT,"&About","Informationaboutthisprogram")看来没问题的是源代码,你自己写的还是有问题,filemenu都没有定义,为什么呢,因为你定义的是filemunu而不是filemenu。。。。。。
⑺ 如何用python写一个简单的12306抢票软件
看看这个可以吗功能:1:全自动爬所有车站列表2:爬所有车站之间的所有车次,过滤重复,保存文件爬过程中保存文件遇到http异常,停止继续脚本代码[python]viewplain#coding:utf-8__author__='watsy'#城市对象classcityObject(object):def__init__(self,abbr_pinyin="",full_pinyin="",chinaname="",shortCode=""):self.abbr_pinyin=abbr_pinyinself.full_piyin=full_pinyinself.chinaname=chinanameself.shortCode=shortCode#火车classtrainObject(object):def__init__(self,tid="",code="",start_city="",start_time="",end_city="",end_time="",full_time=""):self.tid=tidself.code=codeself.start_city=start_cityself.start_time=start_timeself.end_city=end_cityself.end_time=end_timeself.full_time=full_timedefget_writestr(self):#return("%s,%s,%s,%s,%s,%s,%s")%(self.tid,self.code,self.start_city.encode('utf-8'),self.start_time,self.end_city.encode('utf-8'),self.end_time,self.full_time)str_return=self.tid+",";str_return+=self.code+",";str_return+=self.start_city+",";str_return+=self.start_time+",";str_return+=self.end_city+",";str_return+=self.end_time+",";str_return+=self.full_time;returnstr_return#火车列表classtrainModel(list):defisExist(self,train):forsub_traininself:ifsub_train.code==train.code:returnTruereturnFalsedefsave(self):train=self[-1]withopen(("%s.txt")%(train.code),"w")aswf:printtrain.get_writestr()wf.write(train.get_writestr().encode('utf-8'))#解析城市defparserCitys(data):parser_citys=[]fororiginal_cityindata:iforiginal_cityandlen(original_city)>1:split_city=original_city.split('|')parser_city=cityObject(split_city[0],split_city[3],split_city[1],split_city[2])parser_citys.append(parser_city)printlen(parser_citys)returnparser_citys#生成url地址defgetBookingTrainListUrl(start_code,end_code,day):strUrl=("/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&")strUrl+=("orderRequest.train_date=%s&")%(day)strUrl+=("orderRequest.from_station_telecode=%s&")%(start_code)strUrl+=("orderRequest.to_station_telecode=%s&")%(end_code)strUrl+=("orderRequest.train_no=&trainPassType=QB&trainClass=QB%23D%23Z%23T%23K%23QT%23&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str=00%3A00--24%3A00")returnstrUrltrains=trainModel()#解析预定车次列表defparser_booking_str(str_booking):json_book=json.loads(str_booking)datas=json_book['datas']ifdatasandlen(datas)>1:#printdatas.replace("","")trainlist=datas.replace("","").split("\\n")fortrain_strintrainlist:train_str_list=train_str.split(',')iflen(train_str_list)==17:str_id_and_code=train_str_list[1]str_start_city_and_time=train_str_list[2]str_end_city_and_time=train_str_list[3]str_full_time=train_str_list[4]#printstr_id_and_codestr_id=str_id_and_code[13:25]str_code=str_id_and_code[131:-7]#printstr_start_city_and_timeiflen(str_start_city_and_time)>50:str_start_city=str_start_city_and_time[43:-9]else:str_start_city=str_start_city_and_time[0:-9]str_start_time=str_start_city_and_time[-5:]#printstr_end_city_and_timeiflen(str_end_city_and_time)>50:str_end_city=str_end_city_and_time[42:-9]else:str_end_city=str_end_city_and_time[0:-9]str_end_time=str_end_city_and_time[-5:]tobj=trainObject(str_id,str_code,str_start_city,str_start_time,str_end_city,str_end_time,str_full_time)iftrains.isExist(tobj)==False:trains.append(tobj)trains.save()#打开城市列表页面u=urllib2.urlopen("mon/station_name.js?version=1.40")buffer=u.read()u.close()#获取列表buffer=buffer[20:-3]unformatter_citys=buffer.split('@')#得到城市parser_citys=parserCitys(unformatter_citys)city_length=len(parser_citys)today=datetime.date.today()torrow=datetime.timedelta(days=1)today=today+torrowday_str=("%s-%02d-%02d")%(today.year,int(today.month),int(today.day))print(day_str)strPath=os.getcwd()os.chdir("%s/train/"%strPath)foriinrange(1,city_length):forjinrange(0,len(parser_citys)-i):try:print("[%d%d]"%(i,j))sleep(0.09)strurl=getBookingTrainListUrl(parser_citys[i].shortCode,parser_citys[j].shortCode,day_str)url_add_header=urllib2.Request(strurl)url_add_header.add_header('X-Requested-With',"XMLHttpRequest")url_add_header.add_header('Referer',"/otsquery/query/queryRemanentTicketAction.do?method=init")url_add_header.add_header('Content-Type','application/x-www-form-urlencoded')url_add_header.add_header('Connection','keep-alive')resp=urllib2.urlopen(url_add_header)urlread=resp.read()resp.close()parser_booking_str(urlread)excepturllib2.HTTPErroraserr:print("error:[%s]url=[%s]")%(err,strurl)exit(1)os.chdir(strPath)printlen(trains)
⑻ 如何用python写一个简单的12306抢票软件
自问题都详细别给详细答案 抢票软件般模拟浏览器发请求纯粹模拟问题比验证码请求数等等具体看要实现
⑼ python scrapy 能抢票吗
都提示你了scrapy.contrib.spiders已经过时了,请用scrapy.spiders替代from scrapy.contrib.spiders import CrawlSpider换成from scrapy.spiders import CrawlSpider