python selenium simulates browser automatic operation snap-up script

Every time there is a flash sale, I regret that my internet speed and mobile phone speed are too slow to grab the product.

Write a script by hand and let the program help you grab it, and the probability of grabbing it will be greatly increased.

Without further ado, let’s get straight to the code.

This example uses Huawei’s official website to buy mobile phones as an example.

"""
(1) 安装 chromedriver 
a. 去官网 (http://chromedriver.storage.googleapis.com/index.html) 下载对应版本的driver
b. 解压后将exe文件放入本地谷歌浏览器的安装目录 例如: C:\Program Files\Google\Chrome\Application
c. 配置将谷歌安装目录配置到系统环境变量的Path中
(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 re

driver = webdriver.Chrome()
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"
# 提交订单url
submitOrderUrl = "/order/nowConfirmcart"
buyTimeStr = "2023-07-13 20:00:00"


'''
模拟浏览器操作华为官网
@author acheng
@date 2022/10/20 
'''
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()
    # 抢购
    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()
                break
    else :
        # 移动网页
        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("登录成功")


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()无法触发,只能通过执行execute_script执行网页js方法
                driver.execute_script('ec.product.orderNow(null,null,this)')
                # buybn.click()
                canBuy = True
                print("点击下单按钮次数: ".format(buyCountNum))
                buyCountNum += buyCountNum
                break

    if canBuy == True :
        # 预留时间选地址
        time.sleep(15)
        submitBtn = driver.find_element(elementBy.CLASS_NAME,"order-submit-btn")
        if submitBtn is None: 
            print("无法提交订单")              
        else :
            submitBtn.click()

    
    

# 运行主函数
main()

Guess you like

Origin blog.csdn.net/qq_17552823/article/details/131717587