最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

用py写了个针对淘宝的抢购程序(清空购物车)

IT培训 admin 9浏览 0评论

用py写了个针对淘宝的抢购程序(清空购物车)

使用

  • 适合学python的练手
  • 部分代码参考于他人,优化了部分逻辑
  • 手动扫码,把需要的商品提前放入购物车

缺点

  • 取决于网速,提交订单的时候有可能提示拥挤,抢个药应该没问题,抢茅台做梦
  • 结算到提交订单之间的耗时处理的不好,抱砖引玉
import datetime
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.by import By
from selenium.webdriver.support.wait import WebDriverWait启动插件时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
print("启动插件时间::" + 启动插件时间)
抢购时间 = "2022-12-19 16:00:00.000000"from selenium import webdriver
import timedriver = webdriver.Chrome()
timeout = 20  # 等待上限,单位是秒,每个网页加载的上限时间
poll_frequency = 0.8  # 检测的轮询间隔,默认值是0.5秒
#
# driver.implicitly_wait(10)
driver.get("")
# time.sleep(2)
##请登录,他妈的要手动
label1 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.LINK_TEXT, '亲,请登录')))
print("亲,请登录加载完成")
driver.find_element(By.LINK_TEXT, "亲,请登录").click()
##点击二维码登录
# time.sleep(2)
label2 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, 'icon-qrcode')))
print("手动扫码加载完成")driver.find_element(By.CLASS_NAME, "icon-qrcode").click()
##手动扫码这个就很操蛋,要在十秒内完成扫描啊
time.sleep(10)
# driver.implicitly_wait(10)
##去打开购物车
driver.get(".htm")
label3 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.ID, 'J_SelectAll1')))
label4 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, 'submit-btn')))
##此时说明购物车加载出来了
# time.sleep(1)
# 下面去全选购物车
COOMMEN_CONNECT_NUM = 10000000000
count3 = 1
# driver.implicitly_wait(3)
# label3=WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.ID, 'J_SelectAll1')))
# print("全选按钮加载完成")
while True:try:if driver.find_element(By.ID, "J_SelectAll1"):driver.find_element(By.ID, "J_SelectAll1").click()  # 全选print("找到了全选按钮")breakexcept:count3 = count3 + 1driver.refresh()if count3 >= COOMMEN_CONNECT_NUM:print("抢购失败:失败原因:没找到了全选按钮")break
# 此时默认已经选中了全选按钮
time.sleep(1)
# 结算按钮
count2 = 1# 转毫秒
def timeToLong(valid_time):dd = datetime.datetime.strptime(valid_time, '%Y-%m-%d %H:%M:%S.%f')ts = int(time.mktime(dd.timetuple()) * 1000.0 + (dd.microsecond / 1000.0))return ts#
# def str_data_to_num(str_data):
#     # 格式时间成毫秒
#     strptime = time.strptime(str_data,"%Y-%m-%d %H:%M:%S.%f")
#     #print("strptime",strptime)
#     mktime = int(time.mktime(strptime)*1000000)
#     #print("mktime",mktime)
#     return mktimewhile True:##<a href="javascript:void(0)" id="J_Go" class="submit-btn submit-btn-disabled"# aria-label="请注意如果没有选择宝贝,# 将无法结算" data-spm-anchor-id="a1z0d.6639537/tb.0.0"><span>结&nbsp;算</span><b></b></a># <a role="button" title="提交订单" class="go-btn"# style="background-color: rgb(255, 80, 0);">提交订单</a>当前时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")t = timeToLong(抢购时间) - timeToLong(当前时间)print("抢购倒计时%s" % (t))# 网速不好是不是可以调节这个值来减少提交订单的时间误差?if (t < 2505):  # 这里给一秒缓冲提交订单页面的加载时间while True:##去查找结算按钮try:if driver.find_element(By.CLASS_NAME, "submit-btn"):print("找到了结算按钮")driver.find_element(By.CLASS_NAME, "submit-btn").click()breakexcept:count2 = count2 + 1driver.refresh()  # 这里的刷新不知道会不会影响重复查找if (count2 >= COOMMEN_CONNECT_NUM):print("抢购失败:失败原因:没找到了结算按钮")breakbreak##提交订单的逻辑判断
# <a role="button" title="提交订单" class="go-btn"
# style="background-color: rgb(255, 80, 0);">提交订单</a>
# tag = True
# time.sleep(1)
count = 1
# 此时说明提交订单按钮加载出来了try:label5 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, 'go-btn')))
except:print("提交订单的页面加载不出来抢购失败")driver.refresh()  # 刷新time.sleep(1)# 点击人太多了导致此时页面都加载不出来怎么办while True:# if tag == True:#     当前时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")#     print("当前时间" + 当前时间)#     if 当前时间 > qgTime:#         while True:##去查找结算按钮提交订单时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")wucha = timeToLong(抢购时间) - timeToLong(提交订单时间)try:print("==========")if driver.find_element(By.CLASS_NAME, "go-btn"):if wucha < 1000:print("找到了提交订单的按钮")driver.find_element(By.CLASS_NAME, "go-btn").click()thdingdanshijian = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")print("提交订单时间%s" % thdingdanshijian)print("抢购误差%s" % (timeToLong(thdingdanshijian) - timeToLong(抢购时间)))print("抢购成功")# tag = False# 去掉break是因为怕在误差内,提交按钮点不动,让它重复点breakexcept:# count = 1;# while True:#     try:#         if driver.find_element(By.CLASS_NAME, "go-btn"):#             print("老子找到了提交订单的按钮")#             driver.find_element(By.CLASS_NAME, "go-btn").click()#             print("提交订单时间" + 当前时间)#             print("抢购成功")#             break#     except:count = count + 1if count >= COOMMEN_CONNECT_NUM:# 如果连续十次没找到,老子就认为失败print("抢购失败:没找到了提交订单按钮")breaktime.sleep(1000)

用py写了个针对淘宝的抢购程序(清空购物车)

使用

  • 适合学python的练手
  • 部分代码参考于他人,优化了部分逻辑
  • 手动扫码,把需要的商品提前放入购物车

缺点

  • 取决于网速,提交订单的时候有可能提示拥挤,抢个药应该没问题,抢茅台做梦
  • 结算到提交订单之间的耗时处理的不好,抱砖引玉
import datetime
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.by import By
from selenium.webdriver.support.wait import WebDriverWait启动插件时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
print("启动插件时间::" + 启动插件时间)
抢购时间 = "2022-12-19 16:00:00.000000"from selenium import webdriver
import timedriver = webdriver.Chrome()
timeout = 20  # 等待上限,单位是秒,每个网页加载的上限时间
poll_frequency = 0.8  # 检测的轮询间隔,默认值是0.5秒
#
# driver.implicitly_wait(10)
driver.get("")
# time.sleep(2)
##请登录,他妈的要手动
label1 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.LINK_TEXT, '亲,请登录')))
print("亲,请登录加载完成")
driver.find_element(By.LINK_TEXT, "亲,请登录").click()
##点击二维码登录
# time.sleep(2)
label2 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, 'icon-qrcode')))
print("手动扫码加载完成")driver.find_element(By.CLASS_NAME, "icon-qrcode").click()
##手动扫码这个就很操蛋,要在十秒内完成扫描啊
time.sleep(10)
# driver.implicitly_wait(10)
##去打开购物车
driver.get(".htm")
label3 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.ID, 'J_SelectAll1')))
label4 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, 'submit-btn')))
##此时说明购物车加载出来了
# time.sleep(1)
# 下面去全选购物车
COOMMEN_CONNECT_NUM = 10000000000
count3 = 1
# driver.implicitly_wait(3)
# label3=WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.ID, 'J_SelectAll1')))
# print("全选按钮加载完成")
while True:try:if driver.find_element(By.ID, "J_SelectAll1"):driver.find_element(By.ID, "J_SelectAll1").click()  # 全选print("找到了全选按钮")breakexcept:count3 = count3 + 1driver.refresh()if count3 >= COOMMEN_CONNECT_NUM:print("抢购失败:失败原因:没找到了全选按钮")break
# 此时默认已经选中了全选按钮
time.sleep(1)
# 结算按钮
count2 = 1# 转毫秒
def timeToLong(valid_time):dd = datetime.datetime.strptime(valid_time, '%Y-%m-%d %H:%M:%S.%f')ts = int(time.mktime(dd.timetuple()) * 1000.0 + (dd.microsecond / 1000.0))return ts#
# def str_data_to_num(str_data):
#     # 格式时间成毫秒
#     strptime = time.strptime(str_data,"%Y-%m-%d %H:%M:%S.%f")
#     #print("strptime",strptime)
#     mktime = int(time.mktime(strptime)*1000000)
#     #print("mktime",mktime)
#     return mktimewhile True:##<a href="javascript:void(0)" id="J_Go" class="submit-btn submit-btn-disabled"# aria-label="请注意如果没有选择宝贝,# 将无法结算" data-spm-anchor-id="a1z0d.6639537/tb.0.0"><span>结&nbsp;算</span><b></b></a># <a role="button" title="提交订单" class="go-btn"# style="background-color: rgb(255, 80, 0);">提交订单</a>当前时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")t = timeToLong(抢购时间) - timeToLong(当前时间)print("抢购倒计时%s" % (t))# 网速不好是不是可以调节这个值来减少提交订单的时间误差?if (t < 2505):  # 这里给一秒缓冲提交订单页面的加载时间while True:##去查找结算按钮try:if driver.find_element(By.CLASS_NAME, "submit-btn"):print("找到了结算按钮")driver.find_element(By.CLASS_NAME, "submit-btn").click()breakexcept:count2 = count2 + 1driver.refresh()  # 这里的刷新不知道会不会影响重复查找if (count2 >= COOMMEN_CONNECT_NUM):print("抢购失败:失败原因:没找到了结算按钮")breakbreak##提交订单的逻辑判断
# <a role="button" title="提交订单" class="go-btn"
# style="background-color: rgb(255, 80, 0);">提交订单</a>
# tag = True
# time.sleep(1)
count = 1
# 此时说明提交订单按钮加载出来了try:label5 = WebDriverWait(driver, timeout, poll_frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, 'go-btn')))
except:print("提交订单的页面加载不出来抢购失败")driver.refresh()  # 刷新time.sleep(1)# 点击人太多了导致此时页面都加载不出来怎么办while True:# if tag == True:#     当前时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")#     print("当前时间" + 当前时间)#     if 当前时间 > qgTime:#         while True:##去查找结算按钮提交订单时间 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")wucha = timeToLong(抢购时间) - timeToLong(提交订单时间)try:print("==========")if driver.find_element(By.CLASS_NAME, "go-btn"):if wucha < 1000:print("找到了提交订单的按钮")driver.find_element(By.CLASS_NAME, "go-btn").click()thdingdanshijian = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")print("提交订单时间%s" % thdingdanshijian)print("抢购误差%s" % (timeToLong(thdingdanshijian) - timeToLong(抢购时间)))print("抢购成功")# tag = False# 去掉break是因为怕在误差内,提交按钮点不动,让它重复点breakexcept:# count = 1;# while True:#     try:#         if driver.find_element(By.CLASS_NAME, "go-btn"):#             print("老子找到了提交订单的按钮")#             driver.find_element(By.CLASS_NAME, "go-btn").click()#             print("提交订单时间" + 当前时间)#             print("抢购成功")#             break#     except:count = count + 1if count >= COOMMEN_CONNECT_NUM:# 如果连续十次没找到,老子就认为失败print("抢购失败:没找到了提交订单按钮")breaktime.sleep(1000)
发布评论

评论列表 (0)

  1. 暂无评论