每逢秒殺,都在遺憾網速和手速慢沒能搶購到商品吧。
手寫一個腳本,讓程序幫你搶,搶到的概率會大大提升。
廢話不多說,直接上代碼。
本實例以華為官網搶購手機為例
"""
模擬瀏覽器操作華為官網(1) 【只需要安裝一種driver即可】
one: 安裝 chromedriver
a. 去官網 (http://chromedriver.storage.googleapis.com/index.html) 下載對應版本的driver
b. 解壓后將exe文件放入本地谷歌瀏覽器的安裝目錄 例如: C:\Program Files\Google\Chrome\Application
c. 配置將谷歌安裝目錄配置到系統環境變量的Path中
two: 安裝 edgedriver
a.去官網 (https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) 下載對應版本的driver
b. 解壓后將exe文件放入Edge瀏覽器的安裝目錄 例如: C:\Program Files (x86)\Microsoft\Edge\Application(2) 安裝python
(3) 安裝 selenium 指令: pip install selenium
(4) 打開此文件, 修改 賬號參數: userName, password 手機搶購頁面鏈接參數: phonePageUrl 搶購時間參數: buyTimeStr
(5) 設置號華為官網的收貨地址
"""from selenium import webdriver
from selenium.webdriver.common import by
import time
import redriver = webdriver.Chrome()
# driver = webdriver.Edge()
elementBy = by.By()
# 賬號
userName = "134*****38"
password = "l*****6"
# 首頁
indexUrl = "https://www.vmall.com/index.html"
# 想要搶購的手機頁面
# phonePageUrl = "https://www.vmall.com/product/10086213682650.html#2601010448909"
phonePageUrl = "https://www.vmall.com/product/comdetail/index.html?prdId=10086238622707&sbomCode=2601010388226"
# 提交訂單url
submitOrderUrl = "/order/nowConfirmcart"
buyTimeStr = "2023-07-13 20:00:00"def main():# 先登錄login()# 指定時間, 未到時間則阻塞curTime = time.time()buyTime = time.mktime(time.strptime(buyTimeStr,"%Y-%m-%d %H:%M:%S"))while curTime < buyTime : print("當前時間戳:{}, 搶購時間戳:{}".format(curTime,buyTime) , end="\n")time.sleep(10)curTime = time.time()# 搶購if re.search("comdetail", phonePageUrl) is not None :buyTwo()else :buy()def login():print("登錄")driver.get(indexUrl)time.sleep(5)# pc網頁indexLoginBns = driver.find_elements(elementBy.CLASS_NAME, "r-gy4na3")if not indexLoginBns: for bn in indexLoginBns:if bn.text == "請登錄":bn.click()breakelse :# 移動網頁indexLoginBns = driver.find_elements(elementBy.CLASS_NAME,"r-1ff274t")for bn in indexLoginBns:if bn.text == "登錄":bn.click()breaktime.sleep(5)loginElements = driver.find_elements(elementBy.CSS_SELECTOR, ".hwid-input-root")for e in loginElements:inputele = e.find_element(elementBy.TAG_NAME, "input")attr = inputele.get_attribute("type")if attr == "text":inputele.send_keys(userName)elif attr == "password":inputele.send_keys(password)loginBtnElement = driver.find_element(elementBy.CSS_SELECTOR, ".hwid-login-btn")loginBtnElement.click()#需要手機驗證碼 # 此處手動完成驗證碼驗證 預留一分鐘time.sleep(60)print("登錄成功")"""
華為商品頁不統一,此方法為搶購按鈕是立即下單的
"""
def buy():driver.get(phonePageUrl)time.sleep(5)print("打開指定手機 --> 準備搶購")driver.refresh()time.sleep(5)buyBtns = driver.find_elements(elementBy.CLASS_NAME, "product-button02")cur_url = driver.current_urlcanBuy = Falseif buyBtns is None:print("無法獲取下單按鈕")returntime.sleep(2)# 如果沒有進入提交訂單頁面則一直點擊立即下單buyCountNum = 1while not re.search(submitOrderUrl, cur_url) and not canBuy :for buybn in buyBtns:if buybn.find_element(elementBy.TAG_NAME, "span").text == "立即下單" :# 此元素被設置了javascript:; , 所以click()無法觸發,只能通過執行execute_script執行網頁js方法driver.execute_script('ec.product.orderNow(null,null,this)')# buybn.click()canBuy = Trueprint("點擊下單按鈕次數: {}".format(buyCountNum))buyCountNum += buyCountNumbreakif canBuy == True :# 預留時間選地址time.sleep(5)submitBtn = driver.find_element(elementBy.CLASS_NAME,"order-submit-btn")if submitBtn is None: print("無法提交訂單") else :print("提交訂單")# submitBtn.click()driver.execute_script("ec.order.submit();")# 預留時間等待提交響應結束time.sleep(60) """
華為商品頁不統一,此方法為搶購按鈕是立即購買的
此商品詳情每一步都會新開頁面
"""
def buyTwo() :driver.get(phonePageUrl)time.sleep(5)print("打開指定手機 --> 準備搶購")driver.refresh()time.sleep(5)cur_url = driver.current_urlcanBuy = Falsetime.sleep(2)# 如果沒有進入提交訂單頁面則一直點擊立即下單buyCountNum = 1buyButtonScript = """this.document.getElementsByClassName("css-901oao r-jwli3a r-1i10wst r-13uqrnb r-16dba41 r-oxtfae r-rjixqe r-6dt33c r-lrvibr")[0].click();"""while not re.search(submitOrderUrl, cur_url) and not canBuy :# 通過執行execute_script執行網頁js方法driver.execute_script(buyButtonScript)# 預留反應時間time.sleep(2)# 切換到最右邊的網頁窗口driver.switch_to.window(driver.window_handles[-1])cur_url = driver.current_urlprint("點擊下單按鈕次數: {}".format(buyCountNum))buyCountNum += buyCountNumif re.search(submitOrderUrl, cur_url) is not None :canBuy = Truebreakif canBuy == True :# 切換到最右邊的網頁窗口driver.switch_to.window(driver.window_handles[-1])# 預留時間選地址time.sleep(5)submitBtn = driver.find_element(elementBy.CLASS_NAME,"order-submit-btn")if submitBtn is None: print("無法提交訂單") else :print("提交訂單")# submitBtn.click()driver.execute_script("ec.order.submit();")# 預留時間等待提交響應結束time.sleep(60) # 運行主函數
main()