1. 如何用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()
2. 如何用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)
3. 學了bython真的可以製作搶票軟體嗎
這個真可以的,C++和java能編寫的bython也可以編寫,但是你編寫的軟體能不能搶的過那些買票軟體就需要你的伺服器運營和編寫能力了。以前見過一個叫分流搶票的軟體,是一個大神自己編寫的,有興趣可以自己查查看看。他的AI識別做得很好,目前那些亂七八糟的驗證碼需要AI自動識別。
4. 如何用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_
5. 如何寫搶票軟體python實現
你自己的問題都不詳細,別人怎麼會給你詳細的答案。 搶票軟體一般也就是模擬瀏覽器發請求,當然純粹模擬也會有很多問題,比如驗證碼,請求數等等,具體看你要實現什麼效果。
6. 怎麼使用eclisp開發python搶票代碼
我復制你的代碼運行了一遍,沒問題啊。filemunu=wx.Menu()filemenu.Append(wx.ID_ABOUT,"&About","Informationaboutthisprogram")看來沒問題的是源代碼,你自己寫的還是有問題,filemenu都沒有定義,為什麼呢,因為你定義的是filemunu而不是filemenu。。。。。。
7. 我從github上下載了python12306的搶票程序,但運行時一堆錯誤,求各位大神幫幫忙哈
貼一下具體鏈接呢
還有github上的代碼,一般star至少要幾十個的,才有一定質量保證
8. 如何用python寫一個簡單的12306搶票軟體
自問題都詳細別給詳細答案 搶票軟體般模擬瀏覽器發請求純粹模擬問題比驗證碼請求數等等具體看要實現
9. python scrapy 能搶票嗎
都提示你了scrapy.contrib.spiders已經過時了,請用scrapy.spiders替代from scrapy.contrib.spiders import CrawlSpider換成from scrapy.spiders import CrawlSpider
10. 自動化搶票需要刷新網頁嗎 python
夠用就行。東西就是來用的。比如我做web介面測試,我有python基礎知識,會用httplib\urllib2等庫就行了。其實在使用過程中,你遇到問題需要解決,自然會促使你去學習新東西。