Can’t get what you want? Try Python scripts
For Taobao and Tmall, I believe we are already very familiar with them. In the annual Double 11, Double 12, or other shopping festivals, the platform will put on the shelves some goods with high cost performance. The higher the cost performance of goods, the more buyers will naturally attract attention. Have you ever been stuck in the middle of the night waiting for something you want to release, tapping on your phone and running out of stock? This is because there are many snapping robots behind, in competition with you, in front of the program, the eyes see + manual click speed, is undoubtedly slow.
In this article, tech geeks will share a Taobao/Tmall seconds kill script, written in Python, using The Python tripartite library Selenium, and through Selenium + ChromeDriver, using the program control chrome browser, complete a series of operations. With this script, everyone can at least stand on the same starting line with other robots and buy their favorite babies fairly.
Selenium installation and ChromeDriver installation
As we mentioned earlier, selenium libraries are used in Python scripts, and selenium in turn uses chromedriver to control actions such as mouse clicks in the browser. So, our first step is to properly install and configure selenium and chromedriver.
Selenium is simple to install, and like other Python tripartite libraries, we installed it directly with PIP.
pip install selenium
Chromedriver installation, first of all, chromedriver version is very important, we need to select chromedriver version, must be consistent with Chrome version. Therefore, we need to view the Chrome version and type Chrome ://version/ in the browser
In this example, the Chrome version is 72.0.3626. Let’s go to one of the following two websites to download the corresponding Chrome Driver.
Once the download is complete, unzip the package and go to chromedriver.exe and copy it to your Chrome installation directory (you can put it anywhere). Copy the path to the chromedriver.exe file and add it to your computer’s environment variables. Specific:
Enter the environment variable editing interface, add the user variable, double-click PATH, add the file location (for example, C: Program Files (x86) Google Chrome Application\) to the end.
After the installation is complete, go to the CMD interface and enter ChromeDriver. If the chromedriver version number is displayed below, it indicates that the chromedriver is successfully installed.
We also call chromedriver via selenium, the Python library, to simply test chromedriver’s functions. If we have not configured the environment variables, we can test them with the following code
from selenium import webdriver import time def main(): Chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe' #chromedriver file location b = webdriver.Chrome(executable_path = chrome_driver) b.get('https://www.google.com') time.sleep(5) b.quit() if __name__ == From Selenium import webdriver import time def main(): b = webdriver.Chrome() b.get('https://www.baidu.com') time.sleep(5) b.quit() if __name__ == '__main__': main()Copy the code
Core module code parsing
After selenium and chromedriver have been installed and configured, we are ready to write the core logic of the seckill script. Core logic, we implement each step by encapsulating functions. Step 1: Start ChromeDriver and log on to Taobao. What needs to be noted here is that we open taobao’s home page by controlling chromedriver, and after entering the login interface, we need to manually scan the code for login. This step cannot be fully automated.
Step 2: After logging in, we still enter the shopping cart interface through ChromeDriver control, and set a 60-second timed refresh, that is, refresh the shopping cart interface every 60 seconds to keep our Taobao account logged in state. Until the current time and we set the time difference between the baby less than 100 seconds, then enter the next step.
The third step is to select all the goods in the shopping cart through ChromeDriver, calibrate the local time with the taobao server time, and take the return time of Taobao server as the standard time. Once the Taobao time reaches the time of our purchase, immediately submit the order from the shopping cart. At this time, there are two situations. If the task of order submission is sent too early and the order submission button does not appear, we repeat the order submission steps until the button appears. In the other case, the submit order button is already available when we send the submit request, and the submit is successful.
The fourth step is payment. As part of the purchase takes payment as priority, we add the payment operation into the process of seckill script. As the last step, when we successfully submit the order, the script will automatically help us to execute the payment of Alipay. The important thing to note here is that we need to set up the correct 6-digit payment password in advance in the script.
Example of using the kill script
After introducing the core logic of the seckilling script, next, we take Tmall supermarket to buy Maotai liquor as an example to demonstrate the execution process of the seckilling script. Just because we use Moutai as an example doesn’t mean that this script can only snap up moutai, any item added to the shopping cart can be executed at any point at any time using this script. As the daily volume of Maotai supermarket on Tmall is very limited, and there are many plug-ins for all kinds of shopping, the success rate is relatively low. However, for other commodities, after our comparative test, this second kill script will increase the success rate by an order of magnitude compared with manual shopping.
We execute main.py main program script, the program will be controlled by Chromdriver, start Chrome automatically open taobao home page, and enter the login interface. A line at the top of the page warns that Chrome is being controlled by automated testing software.
After logging in, the program will automatically enter the shopping cart interface and refresh every minute to prevent logging in timeout.
When the time of buying is approaching, the program will select the goods in the shopping cart, and automatically click settlement and submit the order when the time of buying arrives.
After submitting the order successfully, we will enter the payment link. If the payment is successful, it means that we have successfully grabbed the desired product in the second kill. If the payment fails, it means that we are unlucky in the second kill and someone grabbed it first. But in general, with the second kill script, we’ve already surpassed the speed of 90% of our users.
① More than 3000 Python ebooks ②Python development environment installation tutorial ③Python400 set self-learning video ④ software development common vocabulary ⑤Python learning roadmap ⑤ project source code case sharing if you use it can be directly taken away in my QQ technical exchange group group number: 754370353 (pure technical exchange and resource sharing, no advertising) to take away by yourself click here to collect