Python version of Mid-Autumn Festival mooncake snapping script

insert image description here

declare

This blog does not provide any server-side programs, nor does it provide any paid snap-up software.
This blog is only for learning selenium automation tools.
If you violate the legitimate rights and interests of any company, please private message me, and the relevant code will be deleted as soon as possible.

foreword

Are there any friends who always sigh that their hands are not as fast as others during the holidays? Things that you clearly like will be sold out in the next second. Don't doubt your speed. Sometimes you don't know whether you are placing an order with a human or a script.
Today, I will share with you a python version of the script to buy moon cakes. We must use magic to defeat magic! Don't say much, just do it.

principle

What I want to use today is a library of testing tools: Selenium
Selenium is an automated testing tool for testing websites. It supports various browsers including Chrome, Firefox, Safari and other mainstream interface browsers, as well as phantomJS interfaceless browsers.
It supports multiple operating systems such as Windows, Linux, IOS, Android, etc.
Today, we are using it to automate the purchase of moon cakes. In fact, we use this tool to "simulate" the corresponding operations of the human browser, such as logging in, selecting items in the shopping cart, placing an order for purchase, and so on.

Install Selenium

way 1

pip install Selenium

way 2

If the pip installation is unsuccessful, you can use the pycharm tool to download

The following data sources can be used in China:
insert image description here
Select an available data source for Selenium

insert image description here

Install the browser driver

download

Selenium3.x calls the browser must have a webdriver driver file, select the following corresponding system chrome to download to download the
Chrome driver file

insert image description here

Configure environment variables

My Computer –> Properties –> System Settings –> Advanced –> Environment Variables –> System Variables –> Path,
add the directory where you downloaded the chromexxx.zip file to the Path value. For example: Path field F:\download

code development

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# 2022/09/03
from selenium import webdriver
import datetime
import time

class Seckill():
    def __init__(self,startTime, choose):
        self.startTime = startTime
        self.choose = choose

    def login(self,browser):
        browser.get("https://www.taobao.com")
        time.sleep(2)
        if browser.find_element_by_link_text("亲,请登录"):
            browser.find_element_by_link_text("亲,请登录").click()
            print("请在15秒内完成扫码")
            time.sleep(15)
            browser.get("https://cart.taobao.com/cart.htm")
        time.sleep(3)
        now = datetime.datetime.now()
        print('登陆成功:', now.strftime('%Y-%m-%d %H:%M:%S'))

    def buy(self,browser):
        if self.choose == 2:
            print("请手动在浏览器中勾选需要秒杀的商品")
        while True:
            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
            # 对比时间,时间到的话就点击结算
            if now > self.startTime:
                if self.choose == 1:
                    while True:
                        try:
                            if browser.find_element_by_id("J_SelectAll2"):
                                browser.find_element_by_id("J_SelectAll2").click()
                                break
                        except:
                            print("error : cant find buy button")
                # 点击结算按钮
                while True:
                    try:
                        if browser.find_element_by_link_text("结 算"):
                            browser.find_element_by_link_text("结 算").click()
                            print("结算成功")
                            break
                    except:
                        pass
                while True:
                    try:
                        if browser.find_element_by_link_text('提交订单'):
                            browser.find_element_by_link_text('提交订单').click()
                            now1 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
                            print("抢购成功时间:%s" % now1)
                    except:
                        print("再次尝试提交订单")
                time.sleep(0.01)


def main():

    pkTime = input("请输入秒杀时间,格式如(2022-09-03 15:40:00.000000):")
    choose = int(input("到时间自动勾选购物车请输入“1”,否则输入“2”:"))
    browser = webdriver.Chrome()
    browser.maximize_window()
    sk = Seckill(pkTime, choose)
    # step1: 登陆
    sk.login(browser)
    # step2: 购买
    sk.buy(browser)

if __name__ == "__main__":
    main()

test

请输入抢购时间,格式如(2022-09-03 15:40:00.000000):2022-09-03 16:17:00.000000
到时间自动勾选购物车请输入“1”,否则输入“2”:1
请在15秒内完成扫码
登陆成功: 2022-09-03 16:14:45
结算成功
再次尝试提交订单
再次尝试提交订单
再次尝试提交订单
再次尝试提交订单
再次尝试提交订单
再次尝试提交订单
再次尝试提交订单
抢购成功时间:2022-09-03 16:17:15.498106

After entering the panic buying time, the browser interface for testing will pop up automatically, and it will automatically adjust to the login interface. At this time, you need to log in by yourself:
insert image description here
After logging in, jump to the shopping cart:
if you choose to automatically check the shopping cart, you will not be able to do so. You need to manually check it, and wait for the program to check it by itself (it is not recommended to choose automatic check), it is
recommended to choose manual check and
insert image description here
wait until you specify the panic buying time, the script will panic purchase within 1 millisecond, place the order and jump to the payment page ,waiting for your payment

Notice

Notice:

  • When testing, please enter "1" when it is time to automatically check the shopping cart, otherwise, enter "2", this option is best to choose 2 (if you choose 1, and your shopping cart has more products, it will automatically check all of them. snapped up)
  • The snap-up time selection is greater than the current time

Guess you like

Origin blog.csdn.net/qq_31557939/article/details/126679134