全自动脚本实现双11秒杀活动

457 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第26天,点击查看活动详情

前言

如今众多的秒杀活动,比如618、双11,想必大家都非常熟悉。平台的大量商品低价秒杀,但总是一秒不到商品就没了。其实在秒杀的背后,有许多机器人在与你竞争,人跟程序比起来,显然是太慢了。把这套脚本拿走,你也能把把秒杀商品。

准备阶段

我们全自动脚本需要下载对应的浏览器驱动,我们使用谷歌浏览器,那就下载谷歌浏览器的驱动chromedriver,查看自己谷歌的版本后,在设置->帮助中就可以找到。

image.png

然后点击连接,下载对应版本的驱动即可。下载连接

下载好chromedriver.exe后,我们需要把它放在放入谷歌浏览器的/Application文件夹下,以及python的执行文件夹下,还需要复制chromedriver.exe文件的路径并加入到电脑的环境变量中去。具体操作如下图:

image.png

image.png

image.png

还需要下载selenium第三方库,可以pip安装,也可以在pycharm->settings中下载。

image.png

下载好后,我们可以通过一个测试代码进行测试


from selenium import webdriver
from selenium.webdriver.chrome.service import Service

if __name__ == '__main__':
    s = Service(r"D:\Google\Chrome\Application\chromedriver.exe")

    driver = webdriver.Chrome(service=s)
    driver.get("https://www.baidu.com")
   

代码实现

实现思路

我们抢购的步骤分为3步

  1. 登录淘宝界面

  2. 进入购物车并全选

  3. 提交订单,订单提交后,我们在手机上付款就行

实现第一步十分简单,我们登录还是用扫码登录,扫码方便,而且用密码登录的话还要处理验证码部分,只要我们扫码登录上去就行。下面是核心代码展示

核心代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2022/11/01
# 淘宝秒杀脚本

import datetime
import time

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.service import Service


# 选择chromedriver.exe路径
s = Service(r"D:\Google\Chrome\Application\chromedriver.exe")
driver = webdriver.Chrome(service=s)


def login():
    driver.get("https://www.taobao.com")


    if driver.find_element(By.LINK_TEXT, "亲,请登录"):
        driver.find_element(By.LINK_TEXT, "亲,请登录").click()

    for i in range(0, 30):
        print("在" + str(30 - i) + "秒内完成扫码")
        time.sleep(1)
        now = datetime.datetime.now()
    print('login success:', now.strftime('%Y-%m-%d %H:%M:%S.%f'))



# 购物车结算
def getcar():
    driver.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()
    time.sleep(3)



def sub_order(ordertime):
    while True:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        print(now)
        if now >= ordertime:
            try:
                # 点击结算按钮
                if driver.find_element(By.ID, "J_Go"):
                    driver.find_element(By.ID, "J_Go").click()
                    now = datetime.datetime.now()
                    print('standby success:', now.strftime('%Y-%m-%d %H:%M:%S.%f'))
                #     点击提交订单
                if driver.find_element(By.CLASS_NAME, 'go-btn'):
                    driver.find_element(By.CLASS_NAME, "go-btn").click()
                    print('order success:' + str(now))
            except:
                pass




if __name__ == '__main__':
    login()
    getcar()
    sub_order('2022-11-11 00:00:00.000000') #输入要抢购的时间即可

提交完订单就结束了,我们用手机支付订单就行了,电脑支付的话支付宝的安全识别绕不过去。。