网络编程
位置:首页>> 网络编程>> Python编程>> Python淘宝秒杀的脚本实现

Python淘宝秒杀的脚本实现

作者:fakerth  发布时间:2022-12-08 01:46:21 

标签:Python,淘宝秒杀

准备工作

我们需要把秒杀的商品加入购物车,因为脚本点击的是全选,所以不需要的商品要移出购物车。

过程分析

1.打开某宝网站;

pq = webdriver.Chrome()
pq.get("https://www.taobao.com")  # 版权问题
time.sleep(3)

sleep的原因是怕万一网速慢,网页加载慢。

2.扫码登陆;

pq.find_element(By.LINK_TEXT, "亲,请登录").click()
print(f"请尽快扫码登录")
time.sleep(10)

自动点击进入登录页面,我们点击扫码登录进行扫码。
3.进入购物车;

pq.get("https://cart.taobao.com/cart.htm")
time.sleep(3)

登录成功后,直接进入购物车网站。

4.全选购物车;

while True:
   try:  # 查找 元素 来自  ID
       if pq.find_element(By.ID, "J_SelectAll1"):
           pq.find_element(By.ID, "J_SelectAll1").click()
           break
   except:
       print(f"找不到购买按钮")

找到全选按钮,全选。

Python淘宝秒杀的脚本实现

5.结算。

while True:
   # 获取电脑现在的时间,                      year month day
   now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
   # 对比时间,时间到的话就点击结算
   print(now)
   if now > lisi:
       # 点击结算按钮
       while True:
           try:
               if pq.find_element(By.LINK_TEXT, "结 算"):
                   print("here")
                   pq.find_element(By.LINK_TEXT, "结 算").click()
                   print(f"主人,程序锁定商品,结算成功")
                   break
           except:
               pass
       while True:
           try:
               if pq.find_element_by_link_text('提交订单'):
                   pq.find_element_by_link_text('提交订单').click()
                   print(f"抢购成功,请尽快付款")
           except:
               print(f"主人,结算提交成功,我已帮你抢到商品啦,请及时支付订单")
               speaker.Speak(f"主人,结算提交成功,我已帮你抢到商品啦,请及时支付订单")
               break
       time.sleep(0.01)

Python淘宝秒杀的脚本实现

获取现在时间与秒杀时间进行比对,时间一到点击提交订单生成订单,生成订单后支付时间就不需要紧张了。

Python淘宝秒杀的脚本实现

完整程序实现如下:

import datetime
import win32com.client
import time
from selenium.webdriver.common.by import By
from selenium import webdriver

now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
speaker = win32com.client.Dispatch("SAPI.SpVoice")

lisi = "2022-11-11 20:00:00.00000000"
zhangsan = webdriver.Chrome()
zhangsan.get("https://www.taobao.com")
time.sleep(3)  # 查找  网络元素 来自 链接 文本(亲,请登录)    #点击
zhangsan.find_element(By.LINK_TEXT, "亲,请登录").click()
print(f"请尽快扫码登录")
time.sleep(10)
zhangsan.get("https://cart.taobao.com/cart.htm")
time.sleep(3)

# 是否全选购物车
while True:
   try:  # 查找 元素 来自  ID
       if zhangsan.find_element(By.ID, "J_SelectAll1"):
           zhangsan.find_element(By.ID, "J_SelectAll1").click()
           break
   except:
       print(f"找不到购买按钮")
while True:
   # 获取电脑现在的时间,                      year month day
   now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
   # 对比时间,时间到的话就点击结算
   print(now)
   if now > lisi:
       # 点击结算按钮
       while True:
           try:
               if zhangsan.find_element(By.LINK_TEXT, "结 算"):
                   print("here")
                   zhangsan.find_element(By.LINK_TEXT, "结 算").click()
                   print(f"主人,程序锁定商品,结算成功")
                   break
           except:
               pass
       while True:
           try:
               if zhangsan.find_element_by_link_text('提交订单'):
                   zhangsan.find_element_by_link_text('提交订单').click()
                   print(f"抢购成功,请尽快付款")
           except:
               print(f"主人,结算提交成功,我已帮你抢到商品啦,请及时支付订单")
               speaker.Speak(f"主人,结算提交成功,我已帮你抢到商品啦,请及时支付订单")
               break
       time.sleep(0.01)

来源:https://blog.csdn.net/weixin_43912621/article/details/127810537

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com