python实现简单淘宝秒杀功能

本文实例为大家分享了Python淘宝秒杀的具体代码,供大家参考,具体内容如下

昨天茅台在线上搞秒杀,本来想着靠我惊人的手速去秒一瓶,结果。。。。

所以痛定思痛,想想还是用脚本更靠谱。就在网上搜啊搜,看到selenium这个,顿时眼睛一亮!

整体流程是:上淘宝->找到登录按钮去登陆->在登录页选择密码登录->填入账号密码登录->跳到购物车页->点击全选按钮,等约定时间,时间到马上结算、下单。

 代码:

import os
from selenium import webdriver
import datetime
import time

#需要下载deckodreiver
firefox = os.path.abspath(r"E:\Program Files\Mozilla Firefox\firefox.exe")
os.environ["webdriver.firefox.bin"] = firefox
driver = webdriver.Firefox()
driver.maximize_window()

def login(uname, pwd):
 driver.get("https://www.taobao.com")
 if driver.find_element_by_link_text("亲,请登录"):
  driver.find_element_by_link_text("亲,请登录").click()
 input(uname, pwd)

 #点击购物车里全选按钮
 if driver.find_element_by_id("J_SelectAll1"):
  driver.find_element_by_id("J_SelectAll1").click()
 # time.sleep(3)
 now = datetime.datetime.now()
 print('login success:', now.strftime('%Y-%m-%d %H:%M:%S'))

def input(uname,pwd):
 time.sleep(3)
 #选择密码登录
 if driver.find_element_by_id("J_Quick2Static"):
  driver.find_element_by_id("J_Quick2Static").click()
 time.sleep(3)

 #用户名输入
 if driver.find_element_by_name("TPL_username"):
  for i in uname:
   driver.find_element_by_name("TPL_username").send_keys(i)
   time.sleep(0.5)
 time.sleep(3)

 #密码输入
 if driver.find_element_by_name("TPL_password"):
  for j in pwd:
   driver.find_element_by_name("TPL_password").send_keys(j)
   time.sleep(0.5)
 time.sleep(3)

 #点击登录按钮
 if driver.find_element_by_id("J_SubmitStatic"):
  driver.find_element_by_id("J_SubmitStatic").click()
 time.sleep(3)
 driver.get("https://cart.taobao.com/cart.htm")
 time.sleep(2)

def buy(buytime):
 while True:
  now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  if now == buytime:
    try:
     #点击结算按钮
     if driver.find_element_by_id("J_Go"):
      driver.find_element_by_id("J_Go").click()
     driver.find_element_by_link_text('提交订单').click()
    except:
     time.sleep(1)
  print(now)
  time.sleep(1)
if __name__=="__main__":
 #中文账号记得decode编码
 login("账号",'密码')
 buy('2018-01-30 13:35:00')

这里用了很多线程休眠,是因为太快的输入,在登录的时候回出现滑动验证框,所以sleep几秒,尽量模仿人的操作。

相关推荐