愛鋒貝

標(biāo)題: python | 在商品頁面秒殺 [打印本頁]

作者: 科技Party    時(shí)間: 2023-3-15 03:49
標(biāo)題: python | 在商品頁面秒殺
(, 下載次數(shù): 17)
上次手寫了一個(gè)在購物車結(jié)算秒殺的腳本,有朋友問有沒有在商品頁秒殺的腳本,于是他來了。
思路

代碼

from selenium import webdriver
from selenium.webdriver.common.by import By
import re
import datetime
import time
import threading

# 啟動瀏覽器的驅(qū)動器
driver = webdriver.Chrome()
# 最大化瀏覽器
driver.maximize_window()

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}

# 需要搶購的商品頁面鏈接
url = "https://detail.tmall.com/item.htm?areaId=610100&cat_id=2&id=42302551887&is_b=1&rn=ee9ddd89a20cdfdbf21dbcf8c4f6a6da&skuId=4228835266195&spm=a220m.1000862.1000725.1.7cdb8dfc4OMuMJ"


def open():
    driver.get(url)
    print('請?jiān)?5秒內(nèi)掃碼登錄')
    time.sleep(15)
    print('請?jiān)?5秒內(nèi)選擇購買商品的規(guī)格')
    time.sleep(15)
    find()


def find():
    # 查找數(shù)量
    if driver.find_element(By.CLASS_NAME, 'quantityTip'):
        text = re.compile('有貨').findall(
            driver.find_element(By.CLASS_NAME, 'quantityTip').text)
        print('text', text)
        if len(text) > 0:
            # 有貨的話,直接購買
            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
            print("有貨時(shí)間:%s" % now)
            buy(len(text))
        else:
            # 定時(shí)器刷新頁面
            driver.refresh()
            timer = threading.Timer(1, find)
            timer.start()


def buy(num):
    # 查找購買按鈕
    if driver.find_elements(By.CLASS_NAME, 'Actions--leftBtn--3kx8kg8'):
        while True:
            # 如果有貨的話
            if num > 0:
                try:
                    driver.find_elements(
                        By.CLASS_NAME, 'Actions--leftBtn--3kx8kg8')[0].click()
                    print('進(jìn)入結(jié)算頁面')
                    time.sleep(0.01)
                    submit()
                except:
                    pass
            break


def submit():
    while True:
        try:
            if driver.find_element(By.LINK_TEXT, '提交訂單'):
                driver.find_element(By.LINK_TEXT, '提交訂單').click()
                now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
                print("搶購成功時(shí)間:%s" % now)
                break
        except:
            print("再次嘗試提交訂單")
            time.sleep(0.01)


open()
結(jié)論


(, 下載次數(shù): 12)
能夠正常搶到商品。明天寫一個(gè)nodejs版本的。

-----------------------------




歡迎光臨 愛鋒貝 (http://m.7gfy2te7.cn/) Powered by Discuz! X3.4