?此例仅用于学习,登录是手动的,模拟抢购时毫秒级内点击生成待支付订单,最后也是需要手动去支付。
请勿商用
# 某平台商品秒杀脚本
from selenium import webdriver
import datetime
import time
# 连接Chrome浏览器
driver = webdriver.Chrome()
def login(url):
# 打开平台登录页,并进行手动登录
driver.get("https://www.网站链接.com")
time.sleep(3)
if driver.find_element_by_link_text("亲,请登录"):
driver.find_element_by_link_text("亲,请登录").click()
print("请在10秒内完成登录") # 手动登录
time.sleep(10)
driver.get(url)
time.sleep(3)
now = datetime.datetime.now()
print('login success:', now.strftime('%Y-%m-%d %H:%M:%S'))
def buy(buytime):
while True:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
# 对比时间,时间到的话就点击结算
if now >= buytime:
try:
# 立即购买
if driver.find_element_by_id("J_LinkBuy"): # F12 点击事件id定位
print("选择商品属性!!!")
# 具体路径根据你购买的商品修改
driver.find_element_by_xpath("//div[1]/div[1]/div/div[4]/div/div/dl[2]/dd/ul/li[1]").click()
time.sleep(0.01)
driver.find_element_by_id("J_LinkBuy").click()
while now >= buytime:
try:
print("订单已生成,赶紧买!!!")
# 提交订单
if driver.find_element_by_class_name('go-btn'):
driver.find_element_by_class_name('go-btn').click()
except:
time.sleep(0.02)
except:
time.sleep(0.08)
print(now)
time.sleep(0.05)
# 抢购主函数
if __name__ == "__main__":
# times = input("请输入抢购时间:时间格式:2022-05-16 19:45:00.000000")
times = '2022-05-16 18:35:00.000000'
# url= '请输入具体商品抢购地址'
url = 'XXX://XXXX.cpm'
login(url)
buy(times)
|