Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money
What’s it like to have a sister who cuts off her hand every Single’s Day? When I did not learn Python, I would give her all kinds of tickets. Now I have learned a little Python, let me write a script for her to buy tickets. Ah! Have what method, be oneself used to that also can spoil only.
After my unremitting efforts, the experiment was finally completed! Welcome to give me a look at the optimization, welcome to point out my shortcomings.
Enter the body…
Our crawling target
Website: Taobao
Tool use
Development tool: pycharm development environment: python3.7, Windows10 use toolkit: selenuim
Demand 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.
Project idea analysis
Selenium Installation and Configuration
Selenium is a tool library for Python automated testing, and you need to configure some environment to drive the Selenium installation
pip install selenium
Copy the code
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. Links:
chromedriver.storage.googleapis.com/index.html
Select the driver version to install based on your Chorme version
Once the download is complete, unzip it and place it in the Python installation directory in the Python folder
Project analysis
Try to open taobao home page through Selenium to check whether the installation is successful
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.taobao.com")
Copy the code
Select the login tag by xpath. The buying script needs to know what you are buying
You can choose the login method by yourself (you can log in with your account password or scan code, which is used here).
After login, click the shopping cart and select the product data you need (optional, you can manually check).
Click the settlement button
Set a clearing time for when you need to place an order
Click the submit order button when the time is up
Easy source sharing
from selenium import webdriver import datetime import time driver = webdriver.Chrome() driver.maximize_window() def login(): Driver. get("https://www.taobao.com") time.sleep(3) if driver.find_element_by_link_text(" parent, please log in "): Driver. Find_element_by_link_text (" kiss, Please login "). Click () print (" please complete within 30 seconds sweep code ") time. Sleep (30) driver. The get (" https://cart.taobao.com/cart.htm ") time. Sleep (# 3) If driver.find_element_by_id("J_SelectAll1"): driver.find_element_by_id("J_SelectAll1").click() now = datetime.datetime.now() print('login success:', now.strftime('%Y-%m-%d %H:%M:%S:%f')) def buy(buytime): while True: Now = datetime.datetime.now().strftime('%Y-%m-%d %H:% m :%S.%f') print(' now ') If driver.find_element_by_id("J_Go"): Driver.find_element_by_id ("J_Go").click() driver.find_element_by_link_text(' submit order ').click() except: The print time. Sleep (0.1) (now) time. Sleep (0.1) if __name__ = = "__main__" : # times = input(" 2018-09-06 11:20:00.000000") # time format: "2018-09-06 11:20:00.000000" login() buy("2021-08-24 14:20:00.000000 ")Copy the code
I am white and white I, a program yuan like to share knowledge ❤️ interested can follow my public account: White and white learning Python [very thank you for your likes, booklets, comments, three links support]