In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to query the remaining train tickets to create a Supreme artifact in Python, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Preface of 0x00
The original price of an AJ1 co-signed OFF-White shoes is 1399, and the speculation price has reached 1W + (actually I am also a SneakerHead). The following is my recent transaction record (used to hold X), which is very gripping. But, now it is a draw, it seems to have nothing to do with the robbery, so I will not write SneakerBot.
Please attach a screenshot of my signature on the NIKE American official website. I heard that Bred Toe has also been a hot search. Ah, ha ha.
So I have this article.
To emphasize, the main purpose of this article is to rob things, from the inquiry of train tickets to the creation of a BOT for Supreme. And it's all based on Python.
0x01 train ticket inquiry
When I went home before, many people worried about train tickets.
Therefore, there is this section of the article. Monitor the remaining tickets on the train.
This environment is: Python2.7+deepinlinux
Because there are a lot of Windows coding problems, I want to hit people, so I changed linux.
The realized effect is like this.
Let's write step by step.
Https://kyfw.12306.cn/otn/leftTicket/init, grab the bag.
This is a Get request. So take a look at this url
Https://kyfw.12306.cn/otn/leftTi... Purpose_codes=ADULT
The train_date parameter is followed by a time
The from_station parameter is followed by the starting station
The to_station parameter is followed by the arrival station.
After analyzing these, we can implement the url through Python.
Here, there is a question, like where to get the English of these urban locations. Through the F12 method, it is found here.
Https://kyfw.12306.cn/otn/resour... Tion_version=1.9047
I climbed these beforehand, and I would like to thank cousin Onise @ 0nise here. In order to ensure the aesthetics of the code, I will import it.
Get the content of the page first
#-*-coding: utf-8-*-import requestsfrom stations import stationsdef 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_textif _ _ name__ = =' _ main__': Get_train_text ()
Then the for loop.
Def Get_train_information (): [/ size] [size=3] for raw_train in Get_train_text (): [/ size] [size=3] print raw_train
Then take a look.
This makes it very clear.
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] # train number from_station_code = data_list [6] # departure station information to_station_code = data_list [ 7] # Terminal information 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] # departure time arrive_time = data_list [9] # arrival time time_duration = data_list [10] # duration first_class_seat = data_list [31] # first class second_class_seat = data_list [30] # second class soft_sleep = data_list [23] or'-'# soft berth hard_sleep = data_list [28] or'-'# hard berth Hard_seat= data_list [29] or'- # hard seat no_seat = data_list [26] or'- -'# No seat
That's about it, and then to make these impressive, we're going to use PrettyTable.
Pip install prettytable
#-*-coding: utf-8-*-import requestsfrom stations import stationsfrom prettytable import PrettyTabledef 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_textdef Get_train_information (): key_list = [] value_list = [] table = PrettyTable () table._set_field_names ('departure time, arrival time, first class, second class soft berth) Hard berth hard seat without seat. 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] # train number from_station_code = data_list [6] # departure station information to_station_code = data_list [ 7] # Terminal information 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] # departure time arrive_time = data_list [9] # arrival time time_duration = data_list [10] # duration first_class_seat = data_list [31] # first class second_class_seat = data_list [30] # second class soft_sleeper = data_list [23] or'-'# soft berth hard_sleeper = data_list [28] or'-'# hard berth Hard_seat= data_list [29] or'--# hard seat no_seat = data_list [26] or'--# No seat 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 tableif _ _ name__ = ='_ main__': Get_train_information ()
Brief introduction of 0x02 Selenium module
After that, we should all use the selenium module in Python. How to put it? I think this thing is very useful.
Here, let me say it briefly.
Installation
Pip install selenium
Install Firefox
Http://ftp.mozilla.org/pub/firefox/releases/
Install FirefoxDriver,geckdriver
Https://github.com/mozilla/geckodriver/releases
Chrome browser needs to install chromedriver,IE browser to install IEdriver
The path of driver can be placed directly under the python path. Here, I put driver under the Scripts path of python and add the script path to the environment variable.
Say a few simple uses..
Visit the page and get the source code
#-*-coding: UTF-8-*-from selenium import webdriver browser = webdriver.Firefox () browser.get ("http://www.baidu.com")print browser.page_source browser.close ()
Browser maximization
Driver.maximize_window ()
Set browser size
Driver.set_window_size (480800)
Browser forward and backward
Driver.back () # browser back driver.forward () # browser forward webdriver provides a series of element positioning methods, commonly used are the following:
Id
Name
Class name
Tag name
Link text
Partial link text
Xpath
Css selector
The corresponding methods in python webdriver are:
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 ()
This is the end of the brief introduction, it is not very detailed.
What about here? You can read the official documents.
Http://selenium-python.readthedocs.io/index.html
0x03 creates its own SupremeBOT
Supreme1994 was born in Manhattan, New York, USA in autumn, and was founded by James Jebbia. The original intention of supreme is supreme and supreme. Supreme is a combination of skateboarding, Hip-hop and other cultures and skateboarding-based American street clothing brand.
So, I'm going to say, it's supreme [su: Suprem], not super me.
It's too expensive to find a purchasing agent, don't get those whose prices are too high, and there are a lot of BOT. So, I have a SUPREMEBOT to build my own, ah, .
I would like to emphasize here that life is too short. I use Python
Or use step-by-step writing, look at nothing else, just look at the train of thought, right
Official website address: http://www.supremenewyork.com/
First of all, let's judge whether the goods exist or not, and add to the shopping cart. I took the accessories of supreme to do the demonstration, without judging SIZE. I will post it later, very simple, a few lines of code. Ah, , I have defined three functions, the first function is to traverse the commodity keywords, the second function is to judge whether the product exists, and the third is the main function (mainly to install X, ah ).
I just posted the code.
#-*-coding: UTF-8-*-import timetry: from selenium import webdriverexcept 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] Commodity found" return True I + = 2 print "[/ I] [/ I] [I] [I] Commodity not found" return Falsedef main (): browser = webdriver.Firefox () browser.implicitly_wait (5) # implicit wait for 5s Invisible wait is to set a maximum waiting time. If the web page is loaded within the specified time, perform the next step, otherwise wait until the time expires. Then execute the next step category = "accessories" # Commodity category keywords = [] keywords.append ("Gold Pendant") # Commodity keyword color = "Gold" # Color 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-1if _ _ name__ = ='_ main__': main ()
Er, let's add a description of the product here. It looks better. Ah, ha ha.
Print "Description:" + links [I] .textprint "Color:" + links [I + 1] .text
If you add the goods to the shopping cart, it's time to pay the bill, isn't it? Ah, , if you fill it out manually, you will definitely miss it, so the effect of selenium has come.
I also defined the function fillForm, in which the receiving information and everything should be changed to your information.
To emphasize, I wrote the official website of the United States. The official website of the United Kingdom seems to have added something to the information. I forgot.
#-*-coding: UTF-8-*-import timetry: from selenium import webdriver from selenium.webdriver.support.ui import Selectexcept 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] 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] Commodity found" Return True i + = 2 print "[/ I] [/ I] [/ I] [I] [I] Commodity not found" return Falsedef fillForm (browser): billing_name = "xxxxx" email = "2014802836@qq.com" tel = "1111111111" billing_address = "xxxxxxxxx" billing_city = "Wauchula" billing_zip = "11111" billing_state = "FL" billing_country = "USA" Nlb = "9999 999999 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) # implicit waiting 5s Invisible wait is to set a maximum waiting time. If the web page is loaded within the specified time, perform the next step, otherwise wait until the time expires. Then execute the next step category = "accessories" # Commodity category keywords = [] keywords.append ("Gold Pendant") # Commodity keyword color = "Gold" # Color 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) # sleep for a second Mainly for fear of making an error [/ 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 ()
And then that's it, and then beautify the CODE.
#-*-coding: UTF-8-*-[/ I] [I] import [/ I] [I] try: [/ I] [I] from selenium import webdriver [/ 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] Commodity found" Return True i + = 2 print "[/ I] [/ I] [/ I] [I] [I] Commodity not found" return Falsedef fillForm (browser): billing_name = "xxxxx" email = "2014802836@qq.com" tel = "1111111111" billing_address = "xxxxxxxxx" billing_city = "Wauchula" billing_zip = "11111" billing_state = "FL" billing_country = "USA" Nlb = "9999 999999 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" [/ I] [/ I] [/ I] [I] Opening Browser... " Browser = webdriver.Firefox () browser.implicitly_wait (5) # implicit wait for 5s. Invisible wait sets a maximum waiting time. If the web page is loaded within the specified time, the next step is executed, otherwise wait until the time expires. Then execute the next step print "[/ I] [/ I] [I] Browser Opened" category = "accessories" # Commodity category keywords = [] keywords.append ("Gold Pendant") # Commodity keyword color = "Gold" # Color 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 time.sleep (1) # sleep for a second Mainly for fear of misreporting 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 ()
Almost like this, you can add a raw_input, so you can save the time to open the browser, right, and finally put the full version, you can grab the hat and so on.
#-*-coding: UTF-8-*-import timetry: from selenium import webdriver from selenium.webdriver.support.ui import Selectexcept 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] 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] Commodity found" Return True i + = 2 print "[/ I] [/ I] [/ I] [I] [I] Commodity not found" return Falsedef fillForm (browser): billing_name = "xxxxx" email = "2014802836@qq.com" tel = "1111111111" billing_address = "xxxxxxxxx" billing_city = "Wauchula" billing_zip = "11111" billing_state = "FL" billing_country = "USA" Nlb = "9999 999999 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" [/ I] [/ I] [/ I] [I] Opening Browser... " Browser = webdriver.Firefox () browser.implicitly_wait (5) # implicit wait for 5s. Invisible wait sets a maximum waiting time. If the web page is loaded within the specified time, the next step is executed, otherwise wait until the time expires. Then execute the next step print "[/ I] [/ I] [/ I] [I] Browser Opened" raw_input ('[/ I] [/ I] [/ I] [I] Press Enter to buy your Commodity') category = "pants" # commodity category keywords = [] keywords.append ("Tiger Stripe Track Pant") # commodity keyword color = "Brown" # Yan Color 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] Commodity sold out." Return-1 try: browser.find_element_by_name ("commit"). Click () except: print "[/ I] [/ I] [/ I] [I] Commodity sold out" return-1 time.sleep (1) # sleep for a second Mainly for fear of misreporting 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 ()
Attached is the result of tonight's week2 grab, let's play a low-key tennis ball, ah ha ha.
After reading the above, do you have any further understanding of how to query the remaining train tickets to build a Supreme artifact in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.