Python uses Selenium to realize the process analysis of Taobao grabbing orders

Recently , in order to fulfill the wish of my little sister, Xiao Ming bought his favorite baby in a treasure, and also became obsessed with python, and realized it easily through python (personal statement: for Java, this is not a betrayal).

Demand analysis & preliminary preparation

The demand is actually very simple, normal shopping. Then our usual shopping process is as follows:


v2-e185d0df4e9ce8c3f861382415f7ddd4_720w.png


Before starting, we need to prepare the program running environment.

  • Environmental system: Windows/Mac OS

  • Python version: 3.7.2

In order to make the program simulate every step of our operation, I will introduce a weapon: Selenium

Selenium test students should feel particularly cordial. It is an automated testing tool, through which we can drive the browser to perform specific actions, such as click, pull down, etc., what you see is what you get.

Selenium installation

Run the following command:

pip install selenium

But if we want to use Selenium to successfully call the Chrome browser to complete the corresponding operation, we also need a medium to interface with the browser. Xiao Ming is using the Chrome browser this time, so he needs to be driven by ChromeDriver.

ChromeDriver installation

Here is the official download address of Google ChromeDriver: https://chromedriver.storage.googleapis.com/index.html We need to find the corresponding version of ChromeDriver according to our Chrome browser version, and then according to the platform type of your computer system Download and decompress.

  • windows: Place it in the Scripts folder under the Python installation path

  • Mac OS: placing in the / usr / local / bin under

This concludes the environment configuration. Xiao Ming wrote a python script to verify the success of the environment setup:

from selenium import webdriver 
  # open Chrome browser 
if __name__ =='__main__': 
  browser = webdriver.Chrome()


After running the code, if you successfully open a new browser, it proves that our environment is installed without problems, and you can shop happily.

Scripting

Comparing the Taobao shopping flowchart above, the corresponding code is as follows:

Open Taobao website

browser.get("https://www.taobao.com")

Login first is to jump to the login page

browser.find_element_by_link_text("Dear, please log in").click()

Login method select QR code

browser.find_element_by_class_name("icon-qrcode").click()

After successfully scanning the code with your mobile phone, proceed to the next step.

After successful login, open the shopping cart

browser.get("https://cart.taobao.com/cart.htm")

Check the product

browser.find_element_by_id("J_SelectAll1").click()

It is recommended to choose select all, but if you have a lot of items in your shopping cart and don’t want to select all to buy, then manually tick the items you want to place an order for now

Guess you like

Origin blog.51cto.com/14825302/2544542