每逢秒杀,都在遗憾网速和手速慢没能抢购到商品吧。
手写一个脚本,让程序帮你抢,抢到的概率会大大提升。
废话不多说,直接上代码。
本实例以华为官网抢购手机为例
"""模拟浏览器操作华为官网(1) 【只需要安装一种driver即可】 one: 安装 chromedriver a. 去官网 (http://chromedriver.storage.googleapis.com/index.html) 下载对应版本的driverb. 解压后将exe文件放入本地谷歌浏览器的安装目录 例如: C:\Program Files\Google\Chrome\Applicationc. 配置将谷歌安装目录配置到系统环境变量的Path中two: 安装 edgedrivera.去官网(https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) 下载对应版本的driverb. 解压后将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 webdriverfrom selenium.webdriver.common import byimport timeimport 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"# 提交订单urlsubmitOrderUrl = "/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 准备抢购")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()