table of Contents:
- introduction
- surroundings
- Requirements Analysis & preparation
- Taobao shopping process review
- Realization spike
- Code combing
- to sum up
0 Preface
618-year shopping spree began, the major electricity supplier began major efforts to discount promotions, our chubby gave us seek a Bo Fuli, APP Taobao search directly: chubby fat welfare, fans receive exclusive three times a day cash a red envelope.
With cash a red envelope, how to do more to save money chop hands? Today, it provides an idea, realize spike orders with Python, automated way to borrow to complete the optimal solution.
1 environment
Operating System: Windows
Python Version: 3.7.2
Needs analysis & preparation
2.0 Requirements Analysis
Our goal is to spike Taobao orders, and there are a few key points, you first need to log in Taobao, secondly you need to prepare the order, the last to submit orders quickly at a specified time.
Login Taobao, here we must use a reptile weapon Selenium, which is an automated testing tool that we can use it drive the browser perform specific actions, such as clicking, drop-down, etc. operations, WYSIWYG. In addition to some JavaScript rendering of the page, the Crawl this way is very effective.
2.1 Selenium installation
Selenium is easy to install, can be used in the following manner.
pip install selenium
After Selenium installed, and can not be used directly, it needs to interface with the browser. Here take Chrome browser, for example. To use Chrome browser Selenium successful call to complete the appropriate action, you need to be driven by ChromeDriver.
2.2 ChromeDriver installation
Here is the official Download ChromeDriver.
link:
https://chromedriver.storage.googleapis.com/index.html
We first before you download Chrome browser version to confirm our use.
Download link ChromeDriver to find the corresponding version of the Chrome browser, download the platform based on the type of your computer system.
After the download is complete, extract, which was allowed to stand at Python installation path to the folder Scripts
After the completion of the operation, we run the following command to test
from selenium import webdriver
# 打开Chrome浏览器
browser = webdriver.Chrome()
After the code is executed, if successful, open the browser, then prove no problem your ChromeDriver installed and working happily using the Selenium.
Next, let's take a look back at Taobao shopping process.
3 Taobao shopping process review
3.1 first open Taobao station
Seleuinm mode change for use, as follows:
browser.get("https://www.taobao.com")
3.2 We log on Taobao to the next step
Seleuinm way into the code:
browser.find_element_by_link_text("亲,请登录").click()
Then we will jump a scan code login page, we scan code using a mobile phone, the next step after a successful login.
3.3 After login is successful, we have to open the shopping cart at the following link:
https://cart.taobao.com/cart.htm
Seleuinm way into the code:
browser.get("https://cart.taobao.com/cart.htm")
3.4 We want to choose a shopping cart full of goods, you can just click Select All
Seleuinm way into the code:
browser.find_element_by_id("J_SelectAll1").click()
Note: If your shopping cart more merchandise, do not want to buy Select All, then manually check the goods you want to order.
3.5 Check after a good product can be "settled" orders
Replaced Seleuinm embodiment, i.e., the code:
browser.find_element_by_link_text("结 算").click()
3.6 submit orders after waiting for completion count
Replaced Seleuinm embodiment, i.e., the code:
browser.find_element_by_link_text('提交订单').click()
After the success of an order under 3.7 seconds, the next payment, slowly enough.
4 spike implementation
Spike realization, the idea is simple. There are two points in time, one is buying time, one of the current time. We need only compare these two time points, to buy time to order now.
Recording time, this need datetime built-in module, as follows:
import datetime
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
5 Code combing
You must first log in Taobao, where we define a login function
def login():
# 打开淘宝首页,通过扫码登录
browser.get("https://www.taobao.com")
time.sleep(3)
if browser.find_element_by_link_text("亲,请登录"):
browser.find_element_by_link_text("亲,请登录").click()
print(f"请尽快扫码登录")
time.sleep(10)
The next step is checking the shopping cart of goods, where we define a picking function
def picking(method):
# 打开购物车列表页面
browser.get("https://cart.taobao.com/cart.htm")
time.sleep(3)
# 是否全选购物车
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"找不到购买按钮")
else:
print(f"请手动勾选需要购买的商品")
time.sleep(5)
Buying time waiting, timed spike, here we define a function buy
def buy(times):
while True:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
# 对比时间,时间到的话就点击结算
if now > times:
# 点击结算按钮
while True:
try:
if browser.find_element_by_link_text("结 算"):
browser.find_element_by_link_text("结 算").click()
print(f"结算成功,准备提交订单")
break
except:
pass
# 点击提交订单按钮
while True:
try:
if browser.find_element_by_link_text('提交订单'):
browser.find_element_by_link_text('提交订单').click()
print(f"抢购成功,请尽快付款")
except:
print(f"再次尝试提交订单")
time.sleep(0.01)
Summary 6
Span of a few lines of code spike Taobao orders quickly into action! Remember, every day searching chubby fat welfare on Taobao, receive exclusive benefits pink yo ~
No public concern " Python column ," replies back, " grab a single device " to obtain a full set of code!