Python
Selenium
自從2022年2月入手沖咖啡坑之後,想要把眾多網友推薦的咖啡豆店家都買一輪試試看
其中有一家特別被咖啡有推崇的厲(囂)害(張)的店家,豆子都要在蝦皮上用搶的…
大概嘗試了3次手速不如人皆失敗收場,於是發揮工程師精神利用程式幫我搶XD
1. 咖啡豆會在每周二、五的早上9:00上架
2. 咖啡豆**賣場的連結會在FB粉絲團提前釋出**(重要!!)
最一開始寫的程式,會遇到一個問題…
就是使用Chromedriver開起的Chromedriver瀏覽器,會是一個全新的乾淨瀏覽器(沒有任何紀錄和GOOGLE帳號登入)
原本第一版程式碼,是會進入蝦皮的登入畫面輸入帳密
但遇到了蝦皮2FA認證直接快不起來,保證搶不到QQ
最快的方式就是操作我們已經登入進蝦皮網站的Chrome瀏覽器視窗
(並且已經把你的取貨地址、收件人等資訊先設定好)
chrome.exe --remote-debugging-port=9527 --user-data-dir="D:\selenium\AutomationProfile"
這句指令是啟動 Chrome瀏覽器的除錯模式,user-data-dir=“D:\selenium\AutomationProfile” 是在單獨的組態檔中啟動Chrome瀏覽器,可以當作是一個新的瀏覽器,埠號可自行指定。
GitHub連結:
https://github.com/mko123654/shopeeScript
我們所要做的只有在開賣前幾分鐘點開程式,擺著,等程式偵測開賣後自動執行XD
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#讓程式控制你已經開啟好,並登入蝦皮的那個Chrome瀏覽器
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9527")
#這裡因為我的chromeDriver安裝在和Chrome.exe同個根目錄下,所以括號內不用指定路徑
driver=webdriver.Chrome(options=options)
#要搶的咖啡頁面連結
driver.get('https://shopee.tw/product/76888890/15579643661/')
#最大等待時間設定為15分鐘(900秒),也就是可以讓你提前執行程式,掛著等待的緩衝時間。
#等"直接購買"avaliable後點選
def do():
WebDriverWait(driver, 900).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class='btn btn-solid-primary btn--l rvHxix']"))).click()
print('------do')
#等"去買單"avaliable後點選,需強制睡一秒最順
def do2():
time.sleep(1)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class='shopee-button-solid shopee-button-solid--primary']"))).click()
print('-----do2')
#等"貨到付款"avaliable後點選(最快的付款方式,千萬不要信用卡,銀行2FA保證讓你搶不到)
def do3():
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='貨到付款']"))).click()
print('-----do3')
#等"下訂單"avaliable後點選
def do4():
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class='stardust-button stardust-button--primary stardust-button--large _1qSlAe']"))).click()
print('-----do4')
#測試先關掉最後"下訂單"
do()
do2()
do3()
# do4()
沒意外的話,一定搶得到喔!!