首頁 > 軟體

python+selenium小米商城紅米K40手機自動搶購的範例程式碼

2021-03-19 19:08:32

使用環境

1、python3
2、selenium

selenium使用簡述

1、安裝selenium

pip install selenium

2、安裝ChromeDriver

下載地址:http://chromedriver.storage.googleapis.com/index.html

注意:下載的ChromeDriver需要與Chrome版本一致。

1)Chrome版本檢視:


2)ChromeDriver對應版本下載:


3)ChromeDriver下載後解壓到任意資料夾,建議可以放到專案目錄中,拷貝chromedriver可執行檔案的路徑,程式碼中需要用到:

程式碼實現

from selenium import webdriver
import time
import datetime


class XiaoMi():
  def __init__(self):
    self.name = "" #登陸小米商城使用者名稱
    self.pwd = "" #登陸小米商城密碼
    self.buytime = "2021-03-12 10:00:00" # 指定秒殺時間,並且開始等待秒殺
    self.chrome_driver = "" #chromedriver的檔案位置 例如:self.chrome_driver = 'C:/Desktop/lib/chromedriver.exe' 

    self.browser = webdriver.Chrome(executable_path = self.chrome_driver)

  def login(self):
    self.browser.get('https://account.xiaomi.com/') # 登入網址
    time.sleep(2)

    self.browser.find_element_by_name("account").send_keys(self.name)
    self.browser.find_element_by_name("password").send_keys(self.pwd)
    self.browser.find_element_by_xpath('//*[@type="submit"]').click()
    time.sleep(3)
    #搶購紅米K40
    self.buy_on_time()

  #搶手機紅米K40
  def buy_on_time(self):
    self.browser.get("https://www.mi.com/buy/detail?product_id=13544") # 切換到秒殺頁面
    time.sleep(2)
    self.browser.find_element_by_xpath('//div[@class="sale-btn"]/a').click() # 再次登陸
    time.sleep(2)
    self.browser.find_element_by_xpath("//div[@class='option-box']/ul/li[4]").click() # 選擇12G+256G版本
    print('登入成功,正在等待搶購···')

    while True: # 不斷重新整理時鐘
      now = datetime.datetime.now()
      if now.strftime('%Y-%m-%d %H:%M:%S') == self.buytime:
        self.browser.find_element_by_xpath('//div[@class="sale-btn"]/a').click() # 購買按鈕的Xpath
        print('下單成功,請抓緊付款!')
      time.sleep(0.01) # 注意重新整理間隔時間要儘量短

if __name__ == '__main__':
  MS = XiaoMi()
  MS.login()

到此這篇關於python+selenium小米商城紅米K40手機自動搶購的範例程式碼的文章就介紹到這了,更多相關python+selenium自動搶購內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com