淘宝抢购程序

淘宝抢购程序

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

开发环境

  • Python
  • 第三方库selenium

摘要

本项目需要提前安装配置好selenium自动化框架。

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

代码

  1. 直接点击"购买"按键进行抢购
# 注意,那个提交订单标签是会变的,用的时候记得要去改一下 # -*- 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")
  1. 在购物车里购买
# 注意,那个提交订单标签是会变的,用的时候记得要去改一下 # -*- 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")
  1. 添加购物车购买
# 注意,那个提交订单标签是会变的,用的时候记得要去改一下 # -*- 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")

__EOF__

本文作者小鹿同学
本文链接https://www.cnblogs.com/exiaolu/p/15943752.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   进击的小鹿  阅读(431)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示