僅供學(xué)習(xí)參考
from selenium import webdriver
import datetime
import time
# ==== 標(biāo)識(shí)登錄狀態(tài)锣夹、重試次數(shù) ====
MAX_LOGIN_RETRY_TIMES = 2
current_retry_login_times = 0
login_success = False
def __login_operates():
driver.get("https://www.taobao.com")
try:
if driver.find_element_by_link_text("親姚炕,請(qǐng)登錄"):
print("沒(méi)登錄仿滔,開(kāi)始點(diǎn)擊登錄按鈕...")
driver.find_element_by_link_text("親伦连,請(qǐng)登錄").click()
print("請(qǐng)使用手機(jī)淘寶掃描屏幕上的二維碼進(jìn)行登錄...")
time.sleep(15)
except:
print("已登錄砰奕,開(kāi)始執(zhí)行跳轉(zhuǎn)...")
global login_success
global current_retry_login_times
login_success = True
current_retry_login_times = 0
def login():
print("開(kāi)始嘗試登錄...")
__login_operates()
global current_retry_login_times
while current_retry_login_times < MAX_LOGIN_RETRY_TIMES:
current_retry_login_times = current_retry_login_times + 1
print("當(dāng)前嘗試登錄次數(shù):" + str(current_retry_login_times))
__login_operates()
if login_success:
print("登錄成功")
break
else:
print("等待登錄中...")
if not login_success:
print("規(guī)定時(shí)間內(nèi)沒(méi)有掃碼登錄淘寶成功墩朦,執(zhí)行失敗,退出腳本!!!")
exit(0)
# time.sleep(3)
now = datetime.datetime.now()
print('login success:', now.strftime('%Y-%m-%d %H:%M:%S'))
def __refresh_keep_alive():
# 重新加載購(gòu)物車(chē)頁(yè)面碗降,定時(shí)操作匿又,防止長(zhǎng)時(shí)間不操作退出登錄
driver.get("https://cart.taobao.com/cart.htm")
print("刷新購(gòu)物車(chē)界面方灾,防止登錄超時(shí)...")
time.sleep(60)
def keep_login_and_wait():
print("當(dāng)前距離搶購(gòu)時(shí)間點(diǎn)還有較長(zhǎng)時(shí)間,開(kāi)始定時(shí)刷新防止登錄超時(shí)...")
while True:
current_time = datetime.datetime.now()
if (buy_time_object - current_time).seconds > 180:
__refresh_keep_alive()
else:
print("搶購(gòu)時(shí)間點(diǎn)將近碌更,停止自動(dòng)刷新裕偿,準(zhǔn)備進(jìn)入搶購(gòu)階段...")
break
def buy():
# 打開(kāi)購(gòu)物車(chē)
driver.get("https://cart.taobao.com/cart.htm")
time.sleep(1)
# 點(diǎn)擊購(gòu)物車(chē)?yán)锶x按鈕
if driver.find_element_by_id("J_SelectAll1"):
driver.find_element_by_id("J_SelectAll1").click()
print("已經(jīng)選中購(gòu)物車(chē)中全部商品 ...")
submit_succ = False
retry_submit_times = 0
while True:
now = datetime.datetime.now()
if now >= buy_time_object:
print("到達(dá)搶購(gòu)時(shí)間,開(kāi)始執(zhí)行搶購(gòu)...嘗試次數(shù):" + str(retry_submit_times))
if submit_succ:
print("訂單已經(jīng)提交成功痛单,無(wú)需繼續(xù)搶購(gòu)...")
break
if retry_submit_times > 50:
print("重試搶購(gòu)次數(shù)達(dá)到上限嘿棘,放棄重試...")
break
retry_submit_times = retry_submit_times + 1
try:
# 點(diǎn)擊結(jié)算按鈕
if driver.find_element_by_id("J_Go"):
driver.find_element_by_id("J_Go").click()
print("已經(jīng)點(diǎn)擊結(jié)算按鈕...")
click_submit_times = 0
while True:
try:
if click_submit_times < 10:
driver.find_element_by_link_text('提交訂單').click()
print("已經(jīng)點(diǎn)擊提交訂單按鈕")
submit_succ = True
break
else:
print("提交訂單失敗...")
except Exception as ee:
# print(ee)
print("沒(méi)發(fā)現(xiàn)提交訂單按鈕,可能頁(yè)面還沒(méi)加載出來(lái)桦他,重試...")
click_submit_times = click_submit_times + 1
time.sleep(0.1)
except Exception as e:
print(e)
print("不好,掛了谆棱,提交訂單失敗了...")
time.sleep(0.5)
if __name__ == '__main__':
# ==== 設(shè)定搶購(gòu)時(shí)間 (修改此處快压,指定搶購(gòu)時(shí)間點(diǎn))====
cur_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
BUY_TIME = input(f"請(qǐng)輸入搶購(gòu)時(shí)間,格式如 {cur_time} :\n")
buy_time_object = datetime.datetime.strptime(BUY_TIME, '%Y-%m-%d %H:%M:%S')
print(f"已設(shè)置搶購(gòu)時(shí)間為:{buy_time_object}")
now_time = datetime.datetime.now()
if now_time > buy_time_object:
print("當(dāng)前已過(guò)搶購(gòu)時(shí)間垃瞧,請(qǐng)確認(rèn)搶購(gòu)時(shí)間是否填錯(cuò)...")
exit(0)
print("正在打開(kāi)chrome瀏覽器...")
# 讓瀏覽器不要顯示當(dāng)前受自動(dòng)化測(cè)試工具控制的提醒
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
#windows
driver = webdriver.Chrome(executable_path='D:\Programs\Python\Python36\chromedriver.exe', chrome_options=option)
#linux
# driver = webdriver.Chrome(executable_path='assets/chromedriver', chrome_options=option)
driver.maximize_window()
print("chrome瀏覽器已經(jīng)打開(kāi)...")
login()
keep_login_and_wait()
buy()