selenium+python自動搶購原始碼
華為MATE10搶購原始碼,使用selenium+python+Chrome
大概流程:
1、按帳號生成多個執行緒開啟瀏覽器
2、登陸帳號密碼,有時需要驗證碼,此處有手動操作
3、不停重新整理搶購頁面,搶購按鈕出現時選擇規格提交訂單
4、排隊頁面等待,直到不在排隊
github:https://github.com/pengsd1991/python_buy_hw
[python] view plain copy print?- # -*- encoding:utf-8 -*-
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- import time
- from threading import Thread
- ACCOUNTS = {
- ”account1”:“password1”
- ”account2”:“password2”
- }
- #MATE10 搶購地址
- #BUY_URL = ‘https://www.vmall.com/product/396602535.html’
- #保時捷
- BUY_URL = ’https://www.vmall.com/product/173840389.html’
- #測試
- # BUY_URL = ‘https://www.vmall.com/product/144380118.html’
- #登入url
- LOGIN_URL = ’https://hwid1.vmall.com/CAS/portal/login.html?validated=true&themeName=red&service=https%3A%2F%2Fwww.vmall.com%2Faccount%2Facaslogin%3Furl%3Dhttps%253A%252F%252Fwww.vmall.com%252F&loginChannel=26000000&reqClientType=26&lang=zh-cn’
- #登入成功手動確認URL
- LOGIN_SUCCESS_CONFIRM = ’https://www.baidu.com/’
- #開始自動重新整理等待搶購按鈕出現的時間點,提前3分鐘
- BEGIN_GO = ’2017-11-16 10:07:10’
- #進到購買頁面後提交訂單
- def submitOrder(driver,user):
- time.sleep(1)
- while BUY_URL == driver.current_url :
- print (user+‘:當前頁面還在商品詳情!!!’)
- time.sleep(3)
- whileTrue:
- try:
- submitOrder = driver.find_element_by_link_text(’提交訂單’)
- submitOrder.click()
- print (user+‘:成功提交訂單’)
- break
- except :
- print (user+‘:提交不了訂單!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!’)
- time.sleep(1)#到了訂單提交頁面提交不了訂單一直等待
- pass
- whileTrue:
- time.sleep(3000)
- print (user+‘:進入睡眠3000s’)
- pass
- #排隊中
- def onQueue(driver,user):
- time.sleep(1)
- nowUrl = driver.current_url
- whileTrue:
- try:
- tryAgain = driver.find_element_by_link_text(’再試一次’)
- tryAgain.click()
- print (user+‘:再試一次點選’)
- pass
- except :
- print (user+‘:排隊中’)
- time.sleep(0.3)#排隊中
- pass
- if nowUrl != driver.current_url:
- print (user+‘:排隊頁面跳轉了!!!!!!!!!!!!!!’)
- break
- submitOrder(driver,user)
- #登入成功去到購買頁面
- def goToBuy(driver,user):
- driver.get(BUY_URL)
- print (user+‘開啟購買頁面’)
- #轉換成搶購時間戳
- timeArray = time.strptime(BEGIN_GO, ”%Y-%m-%d %H:%M:%S”)
- timestamp = time.mktime(timeArray)
- #未發現購買按鈕
- no_found_bug = True
- whileTrue:
- if time.time()>timestamp:#到了搶購時間
- try:
- buyButton = driver.find_element_by_link_text(’立即申購’)
- no_found_bug = False
- print (user+‘立即申購按鈕出現了!!!’)
- #點選摩卡金
- # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[1]/div/ul/li[2]/div/a/p/span’).click()
- #點選6gb+128gb
- # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[3]/div/ul/li[2]/div/a/p/span’).click()
- buyButton.click()
- print (user+‘立即申購’)
- break
- except :
- if no_found_bug:
- time.sleep(0.3)
- if BUY_URL == driver.current_url :#還在當前頁面自動重新整理
- driver.get(BUY_URL)
- pass
- else:
- print(user+‘手動點選了申購’)
- break
- else:
- print (user+‘點選不了申購!!!!!!需要手動點選!!!!!’)
- time.sleep(0.5)
- if BUY_URL != driver.current_url :
- print(user+‘手動點選了申購’)
- break
- pass
- else:
- time.sleep(15)
- print (user+‘睡眠15s,未到指令碼開啟時間:’+BEGIN_GO)
- onQueue(driver,user)
- #登入商城,登陸成功後手動先訪問baidu頁面跳轉至搶購頁面
- def loginMall(user,pwd):
- driver = webdriver.Chrome()
- driver.get(LOGIN_URL)
- hasLogin = False
- try:
- account = driver.find_element_by_xpath(’//*[@id=”login_userName”]’)
- account.click()
- account.send_keys(user)
- time.sleep(1)
- password = driver.find_element_by_xpath(’//*[@id=”login_password”]’)
- password.click()
- password.send_keys(pwd)
- print (user+‘輸入了賬號密碼,等待手動登入’)
- except :
- print (user+‘賬號密碼不能輸入’)
- whileTrue:
- time.sleep(3)
- if LOGIN_SUCCESS_CONFIRM == driver.current_url :
- print(user+‘登入成功!’)
- break
- goToBuy(driver,user)
- if __name__ == “__main__”:
- # 賬號密碼
- data = ACCOUNTS
- # 構建執行緒
- threads = []
- for account, pwd in data.items():
- t = Thread(target=loginMall,args=(account,pwd,))
- threads.append(t)
- # 啟動所有執行緒
- for thr in threads:
- time.sleep(2)
- thr.start()
# -*- encoding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from threading import Thread
ACCOUNTS = { “account1”:“password1” “account2”:“password2” }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
MATE10 搶購地址
保時捷
測試
登入url
登入成功手動確認URL
開始自動重新整理等待搶購按鈕出現的時間點,提前3分鐘
BEGIN_GO = '2017-11-16 10:07:10'
進到購買頁面後提交訂單
def submitOrder(driver,user): time.sleep(1) while BUY_URL == driver.current_url : print (user+':當前頁面還在商品詳情!!!') time.sleep(3)
while True:
try:
submitOrder = driver.find_element_by_link_text('提交訂單')
submitOrder.click()
print (user+':成功提交訂單')
break
except :
print (user+':提交不了訂單!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
time.sleep(1)#到了訂單提交頁面提交不了訂單一直等待
pass
while True:
time.sleep(3000)
print (user+':進入睡眠3000s')
pass
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
排隊中
def onQueue(driver,user): time.sleep(1) nowUrl = driver.current_url while True: try: tryAgain = driver.find_element_by_link_text(‘再試一次’) tryAgain.click() print (user+’:再試一次點選’) pass except : print (user+’:排隊中’) time.sleep(0.3)#排隊中 pass if nowUrl != driver.current_url: print (user+’:排隊頁面跳轉了!!!!!!!!!!!!!!’) break submitOrder(driver,user)
登入成功去到購買頁面
def goToBuy(driver,user): driver.get(BUY_URL) print (user+’開啟購買頁面’) #轉換成搶購時間戳 timeArray = time.strptime(BEGIN_GO, “%Y-%m-%d %H:%M:%S”) timestamp = time.mktime(timeArray) #未發現購買按鈕 no_found_bug = True while True: if time.time()>timestamp:#到了搶購時間 try: buyButton = driver.find_element_by_link_text(‘立即申購’) no_found_bug = False print (user+’立即申購按鈕出現了!!!’) #點選摩卡金 # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[1]/div/ul/li[2]/div/a/p/span’).click() #點選6gb+128gb # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[3]/div/ul/li[2]/div/a/p/span’).click() buyButton.click() print (user+’立即申購’) break except : if no_found_bug: time.sleep(0.3) if BUY_URL == driver.current_url :#還在當前頁面自動重新整理 driver.get(BUY_URL) pass else: print(user+’手動點選了申購’) break else: print (user+’點選不了申購!!!!!!需要手動點選!!!!!’) time.sleep(0.5) if BUY_URL != driver.current_url : print(user+’手動點選了申購’) break pass else: time.sleep(15) print (user+’睡眠15s,未到指令碼開啟時間:’+BEGIN_GO) onQueue(driver,user)
登入商城,登陸成功後手動先訪問baidu頁面跳轉至搶購頁面
def loginMall(user,pwd): driver = webdriver.Chrome() driver.get(LOGIN_URL) hasLogin = False try: account = driver.find_element_by_xpath(‘//*[@id=”login_userName”]’) account.click() account.send_keys(user) time.sleep(1) password = driver.find_element_by_xpath(‘//*[@id=”login_password”]’) password.click() password.send_keys(pwd) print (user+’輸入了賬號密碼,等待手動登入’) except : print (user+’賬號密碼不能輸入’)
while True:
time.sleep(3)
if LOGIN_SUCCESS_CONFIRM == driver.current_url :
print(user+'登入成功!')
break
goToBuy(driver,user)
- 1
- 2
- 3
- 4
- 5
- 6
if name == “main“: # 賬號密碼 data = ACCOUNTS # 構建執行緒 threads = [] for account, pwd in data.items(): t = Thread(target=loginMall,args=(account,pwd,)) threads.append(t) # 啟動所有執行緒 for thr in threads: time.sleep(2) thr.start()
</article>