1. 程式人生 > >Python淘寶秒殺

Python淘寶秒殺

昨天茅臺在線上搞秒殺,本來想著靠我驚人的手速去秒一瓶,結果。。。。

所以痛定思痛,想想還是用指令碼更靠譜。就在網上搜啊搜,看到selenium這個,頓時眼睛一亮!

整體流程是:上淘寶->找到登入按鈕去登陸->在登入頁選擇密碼登入->填入賬號密碼登入->跳到購物車頁->點選全選按鈕,等約定時間,時間到馬上結算、下單

import os
from selenium import webdriver
import datetime
import time

# chromedriver = "C:\\Users\\1\AppData\Local\Google\Chrome\Application\chromedriver.exe"
# os.environ["webdriver.chrome.driver"] = chromedriver
# driver = webdriver.Chrome(chromedriver)
#需要下載Geckodreiver
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)
  try:
    input(uname, pwd)
    print("no except")
  except:
    print("throws exception")
  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__":
  login("xxx",'xxx')
  buy('2018-03-28 00:00:00')

這裡用了很多執行緒休眠,是因為太快的輸入,在登入的時候回出現滑動驗證框,所以sleep幾秒,儘量模仿人的操作