首先慣例,雖然腳本只是模擬網頁點擊,並不是直接調用京東接口,所以並不會對京東的服務器產生破壞,但還是需要提前說一下使用聲明
本賬號發布的項目中涉及的任何腳本,僅用於測試和學習研究,禁止用於商業用途,不能保證其合法性,准確性,完整性和有效性,請根據情況自行判斷。 本項目內所有資源文件,禁止任何公眾號、自媒體進行任何形式的轉載、發布。 suarezz對任何腳本問題概不負責,包括但不限於由任何腳本錯誤導致的任何損失或損害. 間接使用腳本的任何用戶,包括但不限於建立VPS或在某些行為違反國家/地區法律或相關法規的情況下進行傳播, suarezz 對於由此引起的任何隱私泄漏或其他后果概不負責。 請勿將此項目的任何內容用於商業或非法目的,否則后果自負。 如果任何單位或個人認為該項目的腳本可能涉嫌侵犯其權利,則應及時通知並提供身份證明,所有權證明,我們將在收到認證文件后刪除相關腳本。 以任何方式查看此項目的人或直接或間接使用項目的任何腳本的使用者都應仔細閱讀此聲明。suarezz 保留隨時更改或補充此免責聲明的權利。一旦使用並復制了任何相關腳本或項目,則視為您已接受此免責聲明。 您必須在下載后的24小時內從計算機或手機中完全刪除以上內容。
python和selenium就不多介紹了,直接上代碼
from selenium import webdriver import logging import time import datetime import sys from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.desired_capabilities import DesiredCapabilities print("請不要手動刷新此瀏覽器,也不要中途關閉當前瀏覽器,請提前設置你的購物地址,結算界面會選擇默認的購物地址進行結算") #加啟動配置 chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option('excludeSwitches', ['enable-logging']) chrome_options.add_argument('--disable-gpu') # 上面代碼就是為了將Chrome不彈出界面 chrome_options.add_argument('log-level=3')#INFO = 0 WARNING = 1 LOG_ERROR = 2 LOG_FATAL = 3 default is 0 capa = DesiredCapabilities.CHROME capa["pageLoadStrategy"] = "none" #懶加載模式,不等待頁面加載完畢 #去除日志輸出,懶加載的形式啟動谷歌瀏覽器 driver = webdriver.Chrome(desired_capabilities=capa,options=chrome_options) #瀏覽器最大化 driver.maximize_window() #跳轉到京東首頁 driver.get("http://www.jd.com") WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, '你好,請登錄'))) driver.find_element_by_link_text("你好,請登錄").click() print("即將跳轉到登錄頁面,進入登錄倒計時后再開始登錄") time.sleep(1) title = driver.title #准備登陸 for i in range(0,60): if driver.title == title: print("請在%d秒內完成登錄!" % (60 - i)) time.sleep(1) else: break print("完成登錄,准備跳轉到購物車界面") driver.get("https://cart.jd.com/cart.action") print("請輸入搶購時間,精確到秒,示例2021-03-01 10:40:00,這是腳本開始搶購的時間,最好能提前半分鍾左右,最后以回車確認") buytime = input() print("請在搶購時間到達之前保證你的購物車內內容為你需要搶購的內容,以免生成訂單的內容和你需求不相符合") while True: now = datetime.datetime.now() title = driver.title if now.strftime('%Y-%m-%d %H:%M:%S') == buytime: for i in range(1, 10000): print("第%d次確認是否可以勾選" % i) if driver.find_element_by_name('select-all').is_selected()==0: driver.find_element_by_name('select-all').click() else: print("勾選商品成功") break for i in range(1, 10000): print("第%d次確認點擊去結算" % i) if driver.find_element_by_name('select-all').is_selected() == 0: driver.find_element_by_name('select-all').click() WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, '去結算'))) driver.find_element_by_link_text("去結算").click() if (driver.title == title): # 如果檢測到title發生改變,即點擊成功,跳出循環 time.sleep(0.01) else: print("結算成功,頁面正在跳轉") break WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable( (By.ID,'order-submit'))) driver.find_element_by_id("order-submit").click() print('訂購成功,訂單生成成功,請自行付款')