Backend Development 5 min read

Automated Timed Purchase Script for Taobao Using Python and Selenium

This article demonstrates how to build a Python Selenium script that automatically opens a browser, navigates to Taobao, waits for a specified flash‑sale time, and programmatically selects items in the cart, logs in via QR code, and completes the purchase.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Automated Timed Purchase Script for Taobao Using Python and Selenium

The author describes a personal motivation: during a Double‑11 flash sale, his girlfriend missed the discount because she was slow, so he decided to write an automated timed‑purchase script.

Step 1

The script will launch a browser, go to Taobao, stay in the shopping cart, and wait for the purchase time to automatically buy and pay.

Step 2

Import the required modules: a datetime module for the target time and Selenium for browser automation.

<code>import datetime  # module</code>
<code>now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')</code>
<code>import time</code>
<code># Full‑automation Python code</code>
<code>from selenium import webdriver</code>

Step 3

Open Google Chrome and navigate to www.taobao.com , then click the login button to reach the shopping cart.

Code:

<code>times = "2021-11-04 21:00:00.00000000"</code>
<code>browser = webdriver.Chrome()</code>
<code>browser.get("https://www.taobao.com")</code>
<code>time.sleep(3)  # click</code>
<code>browser.find_element_by_link_text("亲,请登录").click()</code>

Because storing account credentials in the script is insecure, the author uses manual QR‑code login.

<code>print(f"请尽快扫码登录")</code>
<code>time.sleep(10)</code>
<code>browser.get("https://cart.taobao.com/cart.htm")</code>
<code>time.sleep(3)</code>

Step 4

After entering the cart, the script waits for the flash‑sale time, selects all items, and proceeds to checkout.

<code># Select all items in the cart</code>
<code>while True:</code>
<code>    try:</code>
<code>        if browser.find_element_by_id("J_SelectAll1"):</code>
<code>            browser.find_element_by_id("J_SelectAll1").click()</code>
<code>            break</code>
<code>    except:</code>
<code>        print(f"找不到购买按钮")</code>

<code># Get current time and compare with target</code>
<code>now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')</code>
<code>print(now)</code>
<code>if now > times:</code>
<code>    # Click the checkout button</code>
<code>    while True:</code>
<code>        try:</code>
<code>            if browser.find_element_by_link_text("结 算"):</code>
<code>                print("here")</code>
<code>                browser.find_element_by_link_text("结 算").click()</code>
<code>                print(f"主人,程序锁定商品,结算成功")</code>
<code>                break</code>
<code>        except:</code>
<code>            pass</code>

<code>    # Click the submit order button</code>
<code>    while True:</code>
<code>        try:</code>
<code>            if browser.find_element_by_link_text('提交订单'):</code>
<code>                browser.find_element_by_link_text('提交订单').click()</code>
<code>                print(f"抢购成功,请尽快付款")</code>
<code>        except:</code>
<code>            print(f"主人,我已帮你抢到商品啦,您来支付吧")</code>
<code>            break</code>
<code>        time.sleep(0.01)</code>

The script assumes the desired items are already in the cart; when the specified time arrives, it selects all, proceeds through checkout, and attempts to submit the order.

Original article link: https://blog.csdn.net/weixin_41556756/article/details/121182499

taobaoflash saleSeleniumscriptWeb Automation
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.