python實現京東商品秒殺功能
阿新 • • 發佈:2021-01-21
from selenium import webdriver import datetime import time # 開啟Chrome瀏覽器 driver = webdriver.Chrome() def auto_buy(username, password, purchase_list_time): print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "開啟登陸介面") driver.get("https://passport.jd.com/new/login.aspx") print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "開始填寫賬號密碼") driver.find_element_by_link_text("賬戶登入").click() driver.find_element_by_name("loginname").send_keys(username) driver.find_element_by_name("nloginpwd").send_keys(password) driver.find_element_by_id("loginsubmit").click() # print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "手動拼圖驗證") # time.sleep(10) #此處睡眠時間用來手動拼圖驗證 print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "登陸成功") print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "等待時間到達搶購時間:", purchase_list_time, "......") while True: count = 0 for buytime in purchase_list_time: nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') if nowtime == buytime: try: count += 1 print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "開始第 %s 次搶購......" % count) print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "開啟購物車並選中商品") driver.get("https://cart.jd.com/cart_index") # 開啟購物車並選中商品 # 如果沒有全選,點選全選 if not driver.find_element_by_class_name('jdcheckbox').is_selected(): driver.find_element_by_class_name('jdcheckbox').click() print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "點選去結算") driver.find_element_by_link_text("去結算").click() # 去結算 print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "點選提交訂單") time.sleep(5) # 提交訂單前必須等待幾秒【感覺跟電腦效能快慢有關,不卡的電腦可以適當降低嘗試】 if driver.find_element_by_id("order-submit"): driver.find_element_by_id("order-submit").click() # 提交訂單 print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "訂單提交成功,請前往訂單中心待付款付款") print("") continue except Exception as e: print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "搶購出現異常,重新搶購: ", e) continue time.sleep(0.001) purchase_list_time = [ "2021-01-20 15:00:00", "2021-01-20 15:00:01", "2021-01-20 15:00:02", "2021-01-20 15:00:03", "2021-01-20 15:00:04", "2021-01-20 15:00:05", ] auto_buy('賬號', '密碼', purchase_list_time)