简介
本文运用dayjs和selenium-webdriver两个包来编写自动秒杀taobao商品的脚本,功能单一简单。
脚本的流程如下:
- 打开浏览器
chrome
,最大化并跳转到taobao.com
- 打开登录页,人工扫码登陆
- 登陆成功跳转到购物车界面
- 自动点击全选按钮
- 自动点击结算按钮
代码
打开命令行工具,安装该脚本所需要的两个包
1 2 3
| //因为作者设置了打赏,需要在末尾加入--no-fund npm i dayjs --save --no-fund npm i selenium-webdriver --save --no fund
|
安装完成后,新建一个.js
文件,名称任意,加入如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| const dayjs = require("dayjs"); const { By } = require("selenium-webdriver"); const selenium = require("selenium-webdriver");
const driver = new selenium.Builder().forBrowser("chrome").build();
driver.manage().window().maximize(); driver.get("https://www.taobao.com"); const sleep = (time) => { return new Promise((resolve) => { setTimeout(resolve, time * 1000); }); };
const login = async () => { const loginText = driver.findElement(By.linkText("亲,请登录")); if (loginText) loginText.click(); console.log("请在20秒内完成扫码"); await sleep(20); driver.get("https://cart.taobao.com/cart.htm"); await sleep(3); if (driver.findElement(By.id("J_SelectAll1"))) driver.findElement(By.id("J_SelectAll1")).click(); console.log("登录成功:", dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")); await sleep(0.4); buy("20xx-xx-xx xx:xx:xx"); };
const buy = async (buyTime) => { while (true) { const now = dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss"); if (now === buyTime) { if (driver.findElement({ id: "J_Go" })) driver.findElement({ id: "J_Go" }).click(); await sleep(0.4); submit(); break; } } };
const submit = async () => { if (driver.findElement(By.linkText("提交订单"))) driver.findElement(By.linkText("提交订单")).click(); console.log("抢购时间:", dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")); await sleep(1000); };
login();
|
运行
在该脚本文件所在位置打开命令行工具,输入:
1 2
| //xxx.js为刚才保存的脚本文件,名字自定 node xxx.js //回车
|
脚本自动运行,扫码登陆后就在你设置好的时间开始帮你秒杀商品。
附 python代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import datetime import time
from selenium.webdriver.common.by import By from selenium import webdriver from selenium.webdriver.chrome.service import Service
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('2023-08-01 11:28:50.000000')
|
参考链接:
https://www.zhihu.com/tardis/bd/art/609639709?source_id=1001