您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Python中如何從火車余票查詢到打造搶Supreme神器,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
0x00 前言
一個AJ1聯名OFF-White的鞋原價1399,炒賣價已經到了1w+(其實我也是個SneakerHead)以下是我近期的交易記錄(拿來裝X的),很扎心.....But,現在都是抽簽,跟搶貌似無關,就不寫SneakerBot了...
再附上我在NIKE美國官網中簽截圖吧,聽說Bred Toe還上了熱搜,啊哈哈
所以就有了本篇文章吧....
強調一下,本文的主要目的就是搶東西,從火車余票查詢,到打造一個搶Supreme的BOT吧。。而這一切都是基于Python
0x01 火車余票查詢
之前回家,不少人為了火車票發愁......
所以,就有了本小節文章....監控火車余票...
本次環境是:Python2.7+deepinlinux
因為Windows編碼問題多的我想打人,所以就換了linux
實現完的效果是這樣的
咱們分步寫
https://kyfw.12306.cn/otn/leftTicket/init,抓包
這是一個Get請求....所以看下這個url
https://kyfw.12306.cn/otn/leftTi ... purpose_codes=ADULT
train_date參數后面是時間
from_station參數后是起點站
to_station參數后是到達站
分析了這些之后,就可以通過Python來實現這個url了
這里呢,有個問題,像這些城市地點的英文在哪獲取,通過F12大法,發現在這
https://kyfw.12306.cn/otn/resour ... tion_version=1.9047
我事先爬好了這些,在這里感謝一下Onise表哥@0nise,為了保證代碼的美觀性吧,所以我就import它了
先獲取頁面內容,
# -*- coding: utf-8 -*- import requests from stations import stations def Get_train_text(): from_station = stations.get(raw_input('Please enter where you are from:')) to_station = stations.get(raw_input('Please enter where you want to go:')) date = raw_input('Please enter the date you want to inquire:') url = ('https://kyfw.12306.cn/otn/leftTicket/queryZ?' 'leftTicketDTO.train_date={}&' 'leftTicketDTO.from_station={}&' 'leftTicketDTO.to_station={}&' 'purpose_codes=ADULT').format(date,from_station,to_station) r=requests.get(url) trains_text = r.json()['data']['result'] print trains_text if __name__ == '__main__': Get_train_text()
然后for循環...
def Get_train_information():[/size] [size=3] for raw_train in Get_train_text():[/size] [size=3] print raw_train
然后觀察一下
這樣就很明朗了吧...
def Get_train_information(): key_list = [] value_list = [] for key,value in stations.items(): key_list.append(key) value_list.append(value) for raw_train in Get_train_text(): data_list = raw_train.split('|') train_number = data_list[3] #車次 from_station_code = data_list[6] #始發站信息 to_station_code = data_list[7] #終點站信息 from_station_index = value_list.index(from_station_code) to_station_index = value_list.index(to_station_code) from_station_name = key_list[from_station_index] to_station_name = key_list[to_station_index] start_time = data_list[8] #出發時間 arrive_time = data_list[9] #到達時間 time_duration = data_list[10] #歷時 first_class_seat = data_list[31] #一等座 second_class_seat = data_list[30] #二等座 soft_sleep = data_list[23] or '--' #軟臥 hard_sleep = data_list[28] or '--' #硬臥 hard_seat= data_list[29] or '--' #硬座 no_seat = data_list[26] or '--' #無座
大概就是這樣,然后讓這些可觀,就要用到PrettyTable了
pip install prettytable
# -*- coding: utf-8 -*- import requests from stations import stations from prettytable import PrettyTable def Get_train_text(): from_station = stations.get(raw_input('Please enter where you are from:')) to_station = stations.get(raw_input('Please enter where you want to go:')) date = raw_input('Please enter the date you want to inquire:') url = ('https://kyfw.12306.cn/otn/leftTicket/queryZ?' 'leftTicketDTO.train_date={}&' 'leftTicketDTO.from_station={}&' 'leftTicketDTO.to_station={}&' 'purpose_codes=ADULT').format(date,from_station,to_station) r=requests.get(url) trains_text = r.json()['data']['result'] return trains_text def Get_train_information(): key_list = [] value_list = [] table = PrettyTable() table._set_field_names('車次 出發地點 到達地點 出發時間 到達時間 歷時 一等 二等 軟臥 硬臥 硬座 無座'.split()) for key,value in stations.items(): key_list.append(key) value_list.append(value) for raw_train in Get_train_text(): data_list = raw_train.split('|') train_number = data_list[3] #車次 from_station_code = data_list[6] #始發站信息 to_station_code = data_list[7] #終點站信息 from_station_index = value_list.index(from_station_code) to_station_index = value_list.index(to_station_code) from_station_name = key_list[from_station_index] to_station_name = key_list[to_station_index] start_time = data_list[8] #出發時間 arrive_time = data_list[9] #到達時間 time_duration = data_list[10] #歷時 first_class_seat = data_list[31] #一等座 second_class_seat = data_list[30] #二等座 soft_sleeper = data_list[23] or '--' #軟臥 hard_sleeper = data_list[28] or '--' #硬臥 hard_seat= data_list[29] or '--' #硬座 no_seat = data_list[26] or '--' #無座 table.add_row([ train_number, from_station_name, to_station_name, start_time, arrive_time, time_duration, first_class_seat, second_class_seat, soft_sleeper, hard_sleeper, hard_seat, no_seat ]) print table if __name__ == '__main__': Get_train_information()
0x02 Selenium模塊簡單介紹
之后的,咱們應該都要用Python中selenium模塊,怎么說呢,這個東西,我感覺挺好用的唉
這里呢,我簡單的說下
安裝
pip install selenium
安裝Firefox
http://ftp.mozilla.org/pub/firefox/releases/
安裝FirefoxDriver,geckdriver
https://github.com/mozilla/geckodriver/releases
Chrome瀏覽器需要安裝chromedriver,IE瀏覽器要安裝IEdriver
driver的路徑可以直接放在python路徑下,我這里把driver放在了python的Scripts路徑下,同時把script路徑加入環境變量。
說幾個簡單用法..
訪問頁面并獲取源碼
#-*- coding: UTF-8 -*- from selenium import webdriver browser = webdriver.Firefox() browser.get("http://www.baidu.com") print browser.page_source browser.close()
瀏覽器最大化
driver.maximize_window()
設置瀏覽器大小
driver.set_window_size(480, 800)
瀏覽器前進后退
driver.back()#瀏覽器后退 driver.forward()#瀏覽器前進
webdriver 提供了一系列的元素定位方法,常用的有以下幾種:
id
name
class name
tag name
link text
partial link text
xpath
css selector
分別對應python webdriver 中的方法為:
find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()
就簡單介紹到這,寫的很不詳細吧....
這里呢?你可以去看官方文檔
http://selenium-python.readthedocs.io/index.html
0x03 打造屬于自己的SupremeBOT
Supreme1994年秋季誕生于美國紐約曼哈頓,由James Jebbia創辦。supreme的本意是最高、至上的。Supreme是結合滑板、Hip-hop等文化并以滑板為主的美國街頭服飾品牌。
那什么,我要說一下,是supreme[su:?pri:m](蘇普瑞姆),而不是super me
找人代購又太貴,價格炒的高的不要不要得,還有一堆BOT。。。。所以呢,我就有了個打造屬于自己得SUPREMEBOT了,啊哈哈
在這里強調一下,人生苦短,我用Python
還是采用分步寫,不看別的,只看思路,對對對
官網地址:http://www.supremenewyork.com/
首先,咱們先判斷,商品是不是存在,并加入購物車,我拿supreme的配件做的演示,沒有加判斷SIZE的。。。我后面會貼上,很簡單的,幾行代碼的事情,啊哈哈我定義了三個函數,第一個函數,是遍歷商品關鍵字的,第二個函數是,判斷商品是否存在的,第三個是main函數(主要是為了裝X吧,啊哈哈)
我直接貼代碼了。。
#-*- coding: UTF-8 -*- import time try: from selenium import webdriver except ImportError: print "Selenium module is not installed...Exiting program." exit(1) def Check(keywords, text): for i in keywords: if i not in text: return False return True def searchCommodity(browser, category, keywords, color): print " Searching Commodity ..." browser.get("http://www.supremenewyork.com/shop/all/" + category) links = browser.find_elements_by_class_name("name-link") i = 0 while i < len(links): if (Check(keywords, links[i].text) & (color in links[i+1].text)): links[i].click() print "[/i][/i][i][i] Commodity found" return True i += 2 print "[/i][/i][i][i] Commodity not found" return False def main(): browser = webdriver.Firefox() browser.implicitly_wait(5) #隱性等待5s,隱形等待是設置了一個最長等待時間,如果在規定時間內網頁加載完成,則執行下一步,否則一直等到時間截止,然后執行下一步 category = "accessories" #商品類別 keywords = [] keywords.append("Gold Pendant") #商品關鍵字 color = "Gold" #顏色 if searchCommodity(browser, category, keywords, color) == False: return -1 try: browser.find_element_by_name("commit").click() except: print "[/i][/i][i][i] Commodity sold out" return -1 if __name__ == '__main__': main()
額,這里加個商品描述吧,好看一點,啊哈哈
print "Description : " + links[i].text print "Color : " + links[i+1].text
把商品加入了購物車了,就該買單了,是不是,啊哈哈,如果手動填,肯定會錯過,所以selenium的功效來了
我又定義了函數fillForm,這里面的收貨信息什么的都要改,改成你的信息
強調一下,我寫的是美國官網,英國官網貌似在信息那多了個啥,忘了。。。。
#-*- coding: UTF-8 -*- import time try: from selenium import webdriver from selenium.webdriver.support.ui import Select except ImportError: print "Selenium module is not installed...Exiting program." exit(1) def Check(keywords, text): for i in keywords: if i not in text: return False return True def searchCommodity(browser, category, keywords, color): print "[/i][/i][/i][i][i][i] Searching Commodity ..." browser.get("http://www.supremenewyork.com/shop/all/" + category) links = browser.find_elements_by_class_name("name-link") i = 0 while i < len(links): if (Check(keywords, links[i].text) & (color in links[i+1].text)): print "Description : " + links[i].text print "Color : " + links[i+1].text links[i].click() print "[/i][/i][/i][/i][/i][/i][i][i][i] Commodity found" return True i += 2 print "[/i][/i][/i][i][i][i] Commodity not found" return False def fillForm(browser): billing_name = "xx xxx" email = "2014802836@qq.com" tel = "1111111111" billing_address = "xxxxxxxxx" billing_city = "Wauchula" billing_zip = "11111" billing_state = "FL" billing_country = "USA" nlb = "9999 999 999 9999" month = "02" year = "2018" rvv = "888" name = browser.find_element_by_name("order[billing_name]").send_keys(billing_name) email = browser.find_element_by_name("order[email]").send_keys(email) tel = browser.find_element_by_name("order[tel]").send_keys(tel) address = browser.find_element_by_name("order[billing_address]").send_keys(billing_address) address = browser.find_element_by_name("order[billing_city]").send_keys(billing_city) postCode = browser.find_element_by_name("order[billing_zip]").send_keys(billing_zip) billing_state = browser.find_element_by_name('order[billing_state]').send_keys(billing_state) countrySelect = Select(browser.find_element_by_name("order[billing_country]")).select_by_visible_text(billing_country) creditCardSelect = browser.find_element_by_name('credit_card[nlb]').send_keys(nlb) monthExpirationSelect = Select(browser.find_element_by_name("credit_card[month]")).select_by_visible_text(month) yearExpirationSelect = Select(browser.find_element_by_name("credit_card[year]")).select_by_visible_text(year) cvv = browser.find_element_by_name("credit_card[rvv]").send_keys(rvv) browser.find_element_by_class_name("terms").click() def main(): browser = webdriver.Firefox() browser.implicitly_wait(5) #隱性等待5s,隱形等待是設置了一個最長等待時間,如果在規定時間內網頁加載完成,則執行下一步,否則一直等到時間截止,然后執行下一步 category = "accessories" #商品類別 keywords = [] keywords.append("Gold Pendant") #商品關鍵字 color = "Gold" #顏色 if searchCommodity(browser, category, keywords, color) == False: return -1 try: browser.find_element_by_name("commit").click() except: print "[/i][/i] Commodity sold out"[/i] [i] return -1[/i] [i] time.sleep(1) #睡一秒,主要是怕報錯[/i] [i] browser.find_element_by_class_name("checkout").click()[/i] [i] fillForm(browser)[/i] [i] browser.find_element_by_name("commit").click()[/i] [i]if __name__ == '__main__':[/i] [i] main()
然后就這么好了,然后美化一下CODE吧
#-*- coding: UTF-8 -*-[/i] [i]import time[/i] [i]try: [/i] [i] from selenium import webdriver[/i] [i] from selenium.webdriver.support.ui import Select[/i] [i]except ImportError:[/i] [i] print "Selenium module is not installed...Exiting program."[/i] [i] exit(1)[/i] [i]def Check(keywords, text):[/i] [i] for i in keywords:[/i] [i] if i not in text:[/i] [i] return False[/i] [i] return True[/i] [i]def searchCommodity(browser, category, keywords, color): [/i] [i] print "[i][i] Searching Commodity ..." browser.get("http://www.supremenewyork.com/shop/all/" + category) links = browser.find_elements_by_class_name("name-link") i = 0 while i < len(links): if (Check(keywords, links[i].text) & (color in links[i+1].text)): print "Description : " + links[i].text print "Color : " + links[i+1].text links[i].click() print "[/i][/i][/i][/i][/i][/i][i][i][i] Commodity found" return True i += 2 print "[/i][/i][/i][i][i][i] Commodity not found" return False def fillForm(browser): billing_name = "xx xxx" email = "2014802836@qq.com" tel = "1111111111" billing_address = "xxxxxxxxx" billing_city = "Wauchula" billing_zip = "11111" billing_state = "FL" billing_country = "USA" nlb = "9999 999 999 9999" month = "02" year = "2018" rvv = "888" name = browser.find_element_by_name("order[billing_name]").send_keys(billing_name) email = browser.find_element_by_name("order[email]").send_keys(email) tel = browser.find_element_by_name("order[tel]").send_keys(tel) address = browser.find_element_by_name("order[billing_address]").send_keys(billing_address) address = browser.find_element_by_name("order[billing_city]").send_keys(billing_city) postCode = browser.find_element_by_name("order[billing_zip]").send_keys(billing_zip) billing_state = browser.find_element_by_name('order[billing_state]').send_keys(billing_state) countrySelect = Select(browser.find_element_by_name("order[billing_country]")).select_by_visible_text(billing_country) creditCardSelect = browser.find_element_by_name('credit_card[nlb]').send_keys(nlb) monthExpirationSelect = Select(browser.find_element_by_name("credit_card[month]")).select_by_visible_text(month) yearExpirationSelect = Select(browser.find_element_by_name("credit_card[year]")).select_by_visible_text(year) cvv = browser.find_element_by_name("credit_card[rvv]").send_keys(rvv) browser.find_element_by_class_name("terms").click() def main(): print " ____ ____ ___ _____ " print "/ ___| _ _ _ __ _ __ ___ _ __ ___ ___| __ ) / _ \_ _|" print "\___ \| | | | '_ \| '__/ _ \ '_ ` _ \ / _ \ _ \| | | || |" print " ___) | |_| | |_) | | | __/ | | | | | __/ |_) | |_| || |" print "|____/ \__,_| .__/|_| \___|_| |_| |_|\___|____/ \___/ |_| " print " |_| " print "[/i][/i][/i][i][i][i] Opening Browser ..." browser = webdriver.Firefox() browser.implicitly_wait(5) #隱性等待5s,隱形等待是設置了一個最長等待時間,如果在規定時間內網頁加載完成,則執行下一步,否則一直等到時間截止,然后執行下一步 print "[/i][/i][/i][i][i][i] Browser Opened" category = "accessories" #商品類別 keywords = [] keywords.append("Gold Pendant") #商品關鍵字 color = "Gold" #顏色 if searchCommodity(browser, category, keywords, color) == False: return -1 try: browser.find_element_by_name("commit").click() except: print "[/i][/i][/i][i][i][i] Commodity sold out" return -1 time.sleep(1) #睡一秒,主要是怕報錯 browser.find_element_by_class_name("checkout").click() print "Filling in the information" fillForm(browser) print "Filled..." print "Prepare to buy a bill....." browser.find_element_by_name("commit").click() print "Finshed,congratulations on your favorite things!!!!!" if __name__ == '__main__': main()
差不多是這樣,可以加個raw_input,這樣就可以省去打開瀏覽器的時間了,對對對,最后放上完全版的,可以搶帽衫什么的
#-*- coding: UTF-8 -*- import time try: from selenium import webdriver from selenium.webdriver.support.ui import Select except ImportError: print "Selenium module is not installed...Exiting program." exit(1) def Check(keywords, text): for i in keywords: if i not in text: return False return True def searchCommodity(browser, category, keywords, color): print "[/i][/i][/i][i][i][i] Searching Commodity ..." browser.get("http://www.supremenewyork.com/shop/all/" + category) links = browser.find_elements_by_class_name("name-link") i = 0 while i < len(links): if (Check(keywords, links[i].text) & (color in links[i+1].text)): print "Description : " + links[i].text print "Color : " + links[i+1].text links[i].click() print "[/i][/i][/i][/i][/i][/i][i][i][i] Commodity found" return True i += 2 print "[/i][/i][/i][i][i][i] Commodity not found" return False def fillForm(browser): billing_name = "xx xxx" email = "2014802836@qq.com" tel = "1111111111" billing_address = "xxxxxxxxx" billing_city = "Wauchula" billing_zip = "11111" billing_state = "FL" billing_country = "USA" nlb = "9999 999 999 9999" month = "02" year = "2018" rvv = "888" name = browser.find_element_by_name("order[billing_name]").send_keys(billing_name) email = browser.find_element_by_name("order[email]").send_keys(email) tel = browser.find_element_by_name("order[tel]").send_keys(tel) address = browser.find_element_by_name("order[billing_address]").send_keys(billing_address) address = browser.find_element_by_name("order[billing_city]").send_keys(billing_city) postCode = browser.find_element_by_name("order[billing_zip]").send_keys(billing_zip) billing_state = browser.find_element_by_name('order[billing_state]').send_keys(billing_state) countrySelect = Select(browser.find_element_by_name("order[billing_country]")).select_by_visible_text(billing_country) creditCardSelect = browser.find_element_by_name('credit_card[nlb]').send_keys(nlb) monthExpirationSelect = Select(browser.find_element_by_name("credit_card[month]")).select_by_visible_text(month) yearExpirationSelect = Select(browser.find_element_by_name("credit_card[year]")).select_by_visible_text(year) cvv = browser.find_element_by_name("credit_card[rvv]").send_keys(rvv) browser.find_element_by_class_name("terms").click() def main(): print " ____ ____ ___ _____ " print "/ ___| _ _ _ __ _ __ ___ _ __ ___ ___| __ ) / _ \_ _|" print "\___ \| | | | '_ \| '__/ _ \ '_ ` _ \ / _ \ _ \| | | || |" print " ___) | |_| | |_) | | | __/ | | | | | __/ |_) | |_| || |" print "|____/ \__,_| .__/|_| \___|_| |_| |_|\___|____/ \___/ |_| " print " |_| " print "[/i][/i][/i][i][i][i] Opening Browser ..." browser = webdriver.Firefox() browser.implicitly_wait(5) #隱性等待5s,隱形等待是設置了一個最長等待時間,如果在規定時間內網頁加載完成,則執行下一步,否則一直等到時間截止,然后執行下一步 print "[/i][/i][/i][i][i][i] Browser Opened" raw_input('[/i][/i][/i][i][i][i]Press Enter to buy your Commodity') category = "pants" #商品類別 keywords = [] keywords.append("Tiger Stripe Track Pant") #商品關鍵字 color = "Brown" #顏色 size = 'Medium' if searchCommodity(browser, category, keywords, color) == False: return -1 if size != "": try: sizeSelect = Select(browser.find_element_by_id("s")) sizeSelect.select_by_visible_text(size) except: print "[/i][/i][/i][i][i][i] Commodity sold out......." return -1 try: browser.find_element_by_name("commit").click() except: print "[/i][/i][/i][i][i][i] Commodity sold out" return -1 time.sleep(1) #睡一秒,主要是怕報錯 browser.find_element_by_class_name("checkout").click() print "Filling in the information" fillForm(browser) print "Filled..." print "Prepare to buy a bill....." browser.find_element_by_name("commit").click() print "Finshed,congratulations on your favorite things!!!!!" if __name__ == '__main__': main()
在附上,今晚week2搶的成果吧,放個低調的網球算了,啊哈哈哈
看完上述內容,你們對Python中如何從火車余票查詢到打造搶Supreme神器有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。