每逢秒杀,都在遗憾网速和手速慢没能抢购到商品吧。
手写一个脚本,让程序帮你抢,抢到的概率会大大提升。
废话不多说,直接上代码。文章来源:https://www.toymoban.com/news/detail-705490.html
本实例以华为官网抢购手机为例文章来源地址https://www.toymoban.com/news/detail-705490.html
"""
模拟浏览器操作华为官网
(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 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网页
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(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_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 :
# 通过执行execute_script执行网页js方法
driver.execute_script(buyButtonScript)
# 预留反应时间
time.sleep(2)
# 切换到最右边的网页窗口
driver.switch_to.window(driver.window_handles[-1])
cur_url = driver.current_url
print("点击下单按钮次数: {}".format(buyCountNum))
buyCountNum += buyCountNum
if re.search(submitOrderUrl, cur_url) is not None :
canBuy = True
break
if 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()
到了这里,关于python selenium 模拟浏览器自动操作抢购脚本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!