淘宝抢购程序

前言

本项目需要提前安装配置好selenium自动化框架。:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


以下是本篇文章正文内容,下面案例可供参考

淘宝抢购程序

本案例是采用selenium自动化测试框架实现。

开发环境

  • Python
  • 第三方库selenium

注意:.find_element_by_xpath在新版中已经被弃用,需要导入from selenium.webdriver.common.by import By,改用.find_element(By.XPATH, “XPATH语法”)来获取标签位置

代码

直接点击"购买"按键进行抢购
# 注意,那个提交订单标签是会变的,用的时候记得要去改一下
# -*- coding: utf-8 -*-
# 直接购买

from selenium import webdriver
import datetime
import time
from os import path
from selenium.webdriver.common.by import By

d = path.dirname(__file__)
abspath = path.abspath(d)

driver = webdriver.Chrome()
driver.maximize_window()


def login():
    # 打开淘宝登录页,并进行扫码登录
    driver.get("https://www.taobao.com")
    time.sleep(3)
    if driver.find_element(By.LINK_TEXT, "亲,请登录"):
        driver.find_element(By.LINK_TEXT, "亲,请登录").click()

    print("请在30秒内完成扫码")
    time.sleep(30)
    # url1 = " "
    url2 = "https://chaoshi.detail.tmall.com/item.htm?spm=a3204.7933263.0.0.1a652258TuF9cG&id=520361968639&rewcatid=51440019&skuId=4689086641091"
    driver.get(url2)
    time.sleep(4)
    driver.find_element(By.XPATH, '//*[@id="J_Amount"]/span[1]/span[1]/span[1]').click()  # 上角标
    now = datetime.datetime.now()
    print('login success:', now.strftime('%Y-%m-%d %H:%M:%S:%f'))

def buy(buytime):
    while True:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        print("当前时间:" + now)
        # 对比时间,时间到的话就点击结算
        if now >= buytime:
            print("开抢.....")
            try:
                if driver.find_element(By.ID, "J_LinkBuy"):  # 点击购买
                    driver.find_element(By.ID, "J_LinkBuy").click()
                    # 点击提交订单按钮
                    while True:
                        try:
                            # time.sleep(1)
                            # if driver.find_element(By.LINK_TEXT,'提交订单'):
                            if driver.find_element(By.XPATH, '//*[@id="submitOrderPC_1"]/div/a'):
                                driver.execute_script('window.scrollBy(0,250)')
                                driver.find_element(By.XPATH, '//*[@id="submitOrderPC_1"]/div/a').click()  # 提交订单
                                print("抢购成功")
                                break
                        except:
                            time.sleep(0.01)
                    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
                    print("主人,我已帮你抢到商品啦,您来支付吧,抢到的时间:" + now)
                    break
            except:
                print("失败")
                time.sleep(0.01)
        time.sleep(0.01)


if __name__ == "__main__":
    # times = input("请输入抢购时间:")
    # 时间格式:"2018-09-06 11:20:00.000000"
    login()
    buy("2022-02-11 22:50:00.000000")

在购物车里购买
# 注意,那个提交订单标签是会变的,用的时候记得要去改一下
# -*- coding: utf-8 -*-
# 在购物车里购买

from selenium import webdriver
import datetime
import time
from os import path
from selenium.webdriver.common.by import By

d = path.dirname(__file__)
abspath = path.abspath(d)

driver = webdriver.Chrome()
driver.maximize_window()


def login():
    # 打开淘宝登录页,并进行扫码登录
    driver.get("https://www.taobao.com")
    time.sleep(3)
    if driver.find_element(By.LINK_TEXT, "亲,请登录"):
        driver.find_element(By.LINK_TEXT, "亲,请登录").click()

    print("请在30秒内完成扫码")
    time.sleep(30)
    url3 = "https://cart.taobao.com/cart.htm"  #  淘宝购物车
    driver.get(url3)
    time.sleep(3)
    # 点击购物车里全选按钮
    if driver.find_element(By.ID,"J_SelectAll1"):
        driver.find_element(By.ID,"J_SelectAll1").click()
    else:
        print("找不到购买按钮,请手动点击商品")
    now = datetime.datetime.now()
    print('login success:', now.strftime('%Y-%m-%d %H:%M:%S:%f'))

def buy(buytime):
    while True:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        # 对比时间,时间到的话就点击结算
        if now >= buytime:
            print("开抢.....")
            while True:
                try:
                    # 点击结算按钮
                    # if driver.find_element(By.ID, "J_Go"):
                    if driver.find_element(By.XPATH, '//*[@class="submit-btn"]/span'):
                        driver.find_element(By.XPATH, '//*[@class="submit-btn"]/span').click()  # 结算
                        print(f"主人,程序锁定商品,结算成功")
                        break
                except:
                    time.sleep(0.01)
            # 点击提交订单按钮
            while True:
                try:
                    # time.sleep(1)
                    # if driver.find_element(By.LINK_TEXT,'提交订单'):
                    if driver.find_element(By.XPATH, '//*[@id="submitOrderPC_1"]/div/a[2]'):
                        driver.execute_script('window.scrollBy(0,250)')
                        driver.find_element(By.XPATH, '//*[@id="submitOrderPC_1"]/div/a[2]').click()  # 提交订单(标签是会变的)
                        print("抢购成功")
                        break
                except:
                    time.sleep(0.01)
            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
            print("主人,我已帮你抢到商品啦,您来支付吧,抢到的时间:" + now)
            break
        print("当前时间:" + now)
        time.sleep(0.01)


if __name__ == "__main__":
    # times = input("请输入抢购时间:")
    # 时间格式:"2018-09-06 11:20:00.000000"
    login()
    buy("2022-02-11 20:06:00.000000")

添加购物车购买
# 注意,那个提交订单标签是会变的,用的时候记得要去改一下
# -*- coding: utf-8 -*-
# 添加购物车购买

from selenium import webdriver
import datetime
import time
from os import path
from selenium.webdriver.common.by import By

d = path.dirname(__file__)
abspath = path.abspath(d)

driver = webdriver.Chrome()
driver.maximize_window()


def login():
    # 打开淘宝登录页,并进行扫码登录
    driver.get("https://www.taobao.com")
    time.sleep(3)
    if driver.find_element(By.LINK_TEXT, "亲,请登录"):
        driver.find_element(By.LINK_TEXT, "亲,请登录").click()

    print("请在30秒内完成扫码")
    time.sleep(30)
    url1 = "https://detail.tmall.com/item.htm?id=614272446185&spm=a21bo.jianhua.201876.1.5af911d95h5At0&scm=1007.34127.211940.0&pvid=369ab0a2-dbec-4618-8e04-b9adcba44189&skuId=4868705035324"
    # url2 = " "
    driver.get(url1)
    time.sleep(4)
    # driver.find_element(By.XPATH,'//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[1]/a/span').click()  # 蓝色
    # time.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="J_Amount"]/span[1]/span[1]/span[1]').click()  # 上角标
    now = datetime.datetime.now()
    print('login success:', now.strftime('%Y-%m-%d %H:%M:%S:%f'))


def buy(buytime):
    while True:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        # 对比时间,时间到的话就点击结算
        if now >= buytime:
            print("开抢.....")
            while True:
                try:
                    if driver.find_element(By.ID, "J_LinkBasket"):  # 加入购物车
                        driver.find_element(By.ID, "J_LinkBasket").click()  # 加入购物车
                        time.sleep(0.1)
                        driver.find_element(By.XPATH, '//*[@id="sn-bd"]/div/ul/li[2]/a').click()  # 点击购物车
                        print("添加购物车成功")
                        break
                except:
                    time.sleep(0.01)
            while True:
                try:
                    if driver.find_element(By.XPATH, '//*[@id="J_SelectAll2"]/div/label'):
                        driver.find_element(By.XPATH, '//*[@id="J_SelectAll2"]/div/label').click()  # 全选
                        break
                except:
                    time.sleep(0.01)
            while True:
                try:
                    if driver.find_element(By.XPATH, '//*[@class="submit-btn"]/span'):
                        driver.find_element(By.XPATH, '//*[@class="submit-btn"]/span').click()   # 结算
                        print("结算成功")
                        break
                except:
                    time.sleep(0.01)
            # 点击提交订单按钮
            while True:
                try:
                    # if driver.find_element(By.LINK_TEXT,'提交订单'):
                    if driver.find_element(By.XPATH, '//*[@id="submitOrderPC_1"]/div/a[2]'):
                        driver.execute_script('window.scrollBy(0,250)')
                        driver.find_element(By.XPATH, '//*[@id="submitOrderPC_1"]/div/a[2]').click()  # 提交订单
                        print("抢购成功")
                        break
                except:
                    time.sleep(0.01)
            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
            print("主人,我已帮你抢到商品啦,您来支付吧,抢到的时间:" + now)
            break
        print("当前时间:" + now)
        time.sleep(0.01)


if __name__ == "__main__":
    # times = input("请输入抢购时间:")
    # 时间格式:"2018-09-06 11:20:00.000000"
    login()
    buy("2022-02-11 20:37:00.000000")

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

進擊的小鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值