Directory:
- The introduction
- The environment
- Requirements analysis & preparation
- Taobao shopping process review
- Implementation of seckill
- Code to comb
- conclusion
0 the introduction
The mid-year shopping carnival on 618 has begun, and major e-commerce companies have launched big discounts and promotions. Our xiao Pang has provided a wave of benefits for everyone. We can search directly on taobao APP: Xiao Pang will give benefits to everyone, and get three big red envelopes of cash for fans every day.
With the big cash red envelope, how to do more money cut hands? Today we provide an idea, using Python to implement the second kill order, using automated way to complete the optimal solution.
1 the environment
Operating system: Windows
Python version: 3.7.2
2. Requirement analysis & Preliminary preparation
2.0 Requirement Analysis
Our goal is to kill taobao orders in seconds. There are several key points in this. First, you need to log in Taobao, second, you need to prepare the order, and finally, you need to submit the order quickly within the specified time.
To log on to Taobao, Selenium, a crawler, is an automated testing tool that allows you to drive the browser to perform certain actions, such as clicking, pulling down, and so on. What you see is what you get. In addition, for some JavaScript rendered pages, this crawling method is very effective.
2.1 Installation of Selenium
Selenium installation is simple and can be done as follows.
pip install selenium
Once Selenium is installed, it cannot be used directly; it needs to be connected to a browser. Take Chrome as an example. To successfully invoke Chrome using Selenium, you need to use ChromeDriver.
2.2 Installing ChromeDriver
Here is the official download address for ChromeDriver.
Links:
chromedriver.storage.googleapis.com/index.html
Let’s check the version of Chrome we’re using before downloading.
Use the ChromeDriver download link to find the corresponding Chrome version and download it based on the platform on which your computer is running.
Once the download is complete, unzip it and place it in the Scripts folder under the Python installation path
After that, run the following command to test it
from selenium import webdriver
# Open Chrome
browser = webdriver.Chrome()
Copy the code
After executing the code, if the browser opens successfully, your ChromeDriver installation is fine and you can use Selenium happily and normally.
Next, let’s review the taobao shopping process first.
3 review of taobao shopping process
3.1 First, open taobao website
www.taobao.com
Instead of using Seleuinm, the code is as follows:
browser.get("https://www.taobao.com")
3.2 We can only go to the next step by logging in taobao
Browser.find_element_by_link_text (" dear, please log in ").click()
At this time we will jump to a scan code login page, we use the phone to scan the code, login success after the next step.
3.3 After successful login, we will open the shopping cart, the link is as follows:
cart.taobao.com/cart.htm
Change to Seleuinm mode, code:
browser.get("https://cart.taobao.com/cart.htm")
3.4 If we want to select all the items in the shopping cart, we can directly click select all
Change to Seleuinm mode, code:
browser.find_element_by_id("J_SelectAll1").click()
Note: if you have a large number of items in your shopping cart and do not want to buy all of them, then manually check the items you want to order.
3.5 After checking the goods, you can “settle” the order
In Seleuinm mode, the code is:
The find_element_by_link_text (" "count"). Click ()
3.6 Wait until the order is submitted
In Seleuinm mode, the code is:
Browser.find_element_by_link_text (' Submit order ').click()
3.7 After the order is successfully placed, the next payment, take your time.
Implementation of 4 seconds kill
The realization of the second kill, the idea is very simple. There are two time points, one is the time to buy, the other is the current time. Just compare the two points and place your order as soon as it’s time to buy.
To record the time, you need to use the built-in datetime module, the code is as follows:
import datetime
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
Copy the code
5 Code Combing
The first step is to login to taobao. Here we define a login function
def login(a):
Open the home page of Taobao and log in by scanning the code
browser.get("https://www.taobao.com")
time.sleep(3)
if browser.find_element_by_link_text("Dear, please log in."):
browser.find_element_by_link_text("Dear, please log in.").click()
print(F "Please scan and log in as soon as possible")
time.sleep(10)
Copy the code
The next step is to check the items in the cart. Here we define a picking function
def picking(method):
Open the shopping cart list page
browser.get("https://cart.taobao.com/cart.htm")
time.sleep(3)
# Select all shopping carts
if method == 0:
while True:
try:
if browser.find_element_by_id("J_SelectAll1"):
browser.find_element_by_id("J_SelectAll1").click()
break
except:
print(F "Can't find buy button")
else:
print(F "Please manually check the items to be purchased")
time.sleep(5)
Copy the code
Wait to buy time, timing seconds kill, here we define a buy function
def buy(times):
while True:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
# Compare the time and click settle when the time is up
if now > times:
Click the settlement button
while True:
try:
if browser.find_element_by_link_text("" is"):
browser.find_element_by_link_text("" is").click()
print(F "Settlement successful, ready to submit the order")
break
except:
pass
Click the Submit order button
while True:
try:
if browser.find_element_by_link_text('Submit order'):
browser.find_element_by_link_text('Submit order').click()
print(F "Purchase success, please pay asap")
except:
print(F "Try to submit the order again")
time.sleep(0.01)
Copy the code
6 summarizes
Just a few dozen lines of code can kill taobao orders in seconds, hurry up!
Pay attention to the public number “Python column”, background reply “grab a single device” to get a full set of code!