每逢秒殺,都在遺憾網(wǎng)速和手速慢沒能搶購到商品吧。
手寫一個腳本,讓程序幫你搶,搶到的概率會大大提升。
廢話不多說,直接上代碼。文章來源:http://www.zghlxwxcb.cn/news/detail-705490.html
本實例以華為官網(wǎng)搶購手機為例文章來源地址http://www.zghlxwxcb.cn/news/detail-705490.html
"""
模擬瀏覽器操作華為官網(wǎng)
(1) 【只需要安裝一種driver即可】
one: 安裝 chromedriver
a. 去官網(wǎng) (http://chromedriver.storage.googleapis.com/index.html) 下載對應版本的driver
b. 解壓后將exe文件放入本地谷歌瀏覽器的安裝目錄 例如: C:\Program Files\Google\Chrome\Application
c. 配置將谷歌安裝目錄配置到系統(tǒng)環(huán)境變量的Path中
two: 安裝 edgedriver
a.去官網(wǎng) (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) 打開此文件, 修改 賬號參數(shù): userName, password 手機搶購頁面鏈接參數(shù): phonePageUrl 搶購時間參數(shù): buyTimeStr
(5) 設置號華為官網(wǎng)的收貨地址
"""
from selenium import webdriver
from selenium.webdriver.common import by
import time
import re
driver = 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網(wǎng)頁
indexLoginBns = driver.find_elements(elementBy.CLASS_NAME, "r-gy4na3")
if not indexLoginBns:
for bn in indexLoginBns:
if bn.text == "請登錄":
bn.click()
break
else :
# 移動網(wǎng)頁
indexLoginBns = driver.find_elements(elementBy.CLASS_NAME,"r-1ff274t")
for bn in indexLoginBns:
if bn.text == "登錄":
bn.click()
break
time.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("登錄成功")
"""
華為商品頁不統(tǒng)一,此方法為搶購按鈕是立即下單的
"""
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_url
canBuy = False
if buyBtns is None:
print("無法獲取下單按鈕")
return
time.sleep(2)
# 如果沒有進入提交訂單頁面則一直點擊立即下單
buyCountNum = 1
while not re.search(submitOrderUrl, cur_url) and not canBuy :
for buybn in buyBtns:
if buybn.find_element(elementBy.TAG_NAME, "span").text == "立即下單" :
# 此元素被設置了javascript:; , 所以click()無法觸發(fā),只能通過執(zhí)行execute_script執(zhí)行網(wǎng)頁js方法
driver.execute_script('ec.product.orderNow(null,null,this)')
# buybn.click()
canBuy = True
print("點擊下單按鈕次數(shù): {}".format(buyCountNum))
buyCountNum += buyCountNum
break
if 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();")
# 預留時間等待提交響應結(jié)束
time.sleep(60)
"""
華為商品頁不統(tǒng)一,此方法為搶購按鈕是立即購買的
此商品詳情每一步都會新開頁面
"""
def buyTwo() :
driver.get(phonePageUrl)
time.sleep(5)
print("打開指定手機 --> 準備搶購")
driver.refresh()
time.sleep(5)
cur_url = driver.current_url
canBuy = False
time.sleep(2)
# 如果沒有進入提交訂單頁面則一直點擊立即下單
buyCountNum = 1
buyButtonScript = """
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 :
# 通過執(zhí)行execute_script執(zhí)行網(wǎng)頁js方法
driver.execute_script(buyButtonScript)
# 預留反應時間
time.sleep(2)
# 切換到最右邊的網(wǎng)頁窗口
driver.switch_to.window(driver.window_handles[-1])
cur_url = driver.current_url
print("點擊下單按鈕次數(shù): {}".format(buyCountNum))
buyCountNum += buyCountNum
if re.search(submitOrderUrl, cur_url) is not None :
canBuy = True
break
if canBuy == True :
# 切換到最右邊的網(wǎng)頁窗口
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();")
# 預留時間等待提交響應結(jié)束
time.sleep(60)
# 運行主函數(shù)
main()
到了這里,關于python selenium 模擬瀏覽器自動操作搶購腳本的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!