1. 程式人生 > >selenium+python自動搶購原始碼

selenium+python自動搶購原始碼

[python] view plain copy print?

華為MATE10搶購原始碼,使用selenium+python+Chrome

大概流程:

1、按帳號生成多個執行緒開啟瀏覽器

2、登陸帳號密碼,有時需要驗證碼,此處有手動操作

3、不停重新整理搶購頁面,搶購按鈕出現時選擇規格提交訂單

4、排隊頁面等待,直到不在排隊

github:https://github.com/pengsd1991/python_buy_hw

[python] view plain copy print?
  1. # -*- encoding:utf-8 -*-
  2. from selenium import webdriver  
  3. from selenium.webdriver.common.keys import Keys  
  4. import time  
  5. from threading import Thread  
  6. ACCOUNTS = {  
  7.         ”account1”:“password1”
  8.         ”account2”:“password2”
  9.     }  
  10. #MATE10 搶購地址
  11. #BUY_URL = ‘https://www.vmall.com/product/396602535.html’
  12. #保時捷
  13. BUY_URL = ’https://www.vmall.com/product/173840389.html’
  14. #測試
  15. # BUY_URL = ‘https://www.vmall.com/product/144380118.html’
  16. #登入url
  17. 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’
  18. #登入成功手動確認URL
  19. LOGIN_SUCCESS_CONFIRM = ’https://www.baidu.com/’
  20. #開始自動重新整理等待搶購按鈕出現的時間點,提前3分鐘
  21. BEGIN_GO = ’2017-11-16 10:07:10’
  22. #進到購買頁面後提交訂單
  23. def submitOrder(driver,user):  
  24.     time.sleep(1)  
  25.     while BUY_URL == driver.current_url :  
  26.         print (user+‘:當前頁面還在商品詳情!!!’)  
  27.         time.sleep(3)  
  28.     whileTrue:  
  29.         try:  
  30.             submitOrder = driver.find_element_by_link_text(’提交訂單’)  
  31.             submitOrder.click()  
  32.             print (user+‘:成功提交訂單’)  
  33.             break
  34.         except :  
  35.             print (user+‘:提交不了訂單!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!’)  
  36.             time.sleep(1)#到了訂單提交頁面提交不了訂單一直等待 
  37.             pass
  38.     whileTrue:  
  39.         time.sleep(3000)  
  40.         print (user+‘:進入睡眠3000s’)  
  41.         pass
  42. #排隊中
  43. def onQueue(driver,user):  
  44.     time.sleep(1)  
  45.     nowUrl = driver.current_url  
  46.     whileTrue:  
  47.         try:  
  48.             tryAgain = driver.find_element_by_link_text(’再試一次’)  
  49.             tryAgain.click()  
  50.             print (user+‘:再試一次點選’)  
  51.             pass
  52.         except :  
  53.             print (user+‘:排隊中’)  
  54.             time.sleep(0.3)#排隊中
  55.             pass
  56.         if nowUrl != driver.current_url:  
  57.             print (user+‘:排隊頁面跳轉了!!!!!!!!!!!!!!’)  
  58.             break
  59.     submitOrder(driver,user)  
  60. #登入成功去到購買頁面
  61. def goToBuy(driver,user):  
  62.     driver.get(BUY_URL)  
  63.     print (user+‘開啟購買頁面’)  
  64.     #轉換成搶購時間戳
  65.     timeArray = time.strptime(BEGIN_GO, ”%Y-%m-%d %H:%M:%S”)  
  66.     timestamp = time.mktime(timeArray)  
  67.     #未發現購買按鈕
  68.     no_found_bug = True
  69.     whileTrue:  
  70.         if time.time()>timestamp:#到了搶購時間
  71.             try:  
  72.                 buyButton = driver.find_element_by_link_text(’立即申購’)  
  73.                 no_found_bug = False
  74.                 print (user+‘立即申購按鈕出現了!!!’)  
  75.                 #點選摩卡金
  76.                 # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[1]/div/ul/li[2]/div/a/p/span’).click()
  77.                 #點選6gb+128gb
  78.                 # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[3]/div/ul/li[2]/div/a/p/span’).click()
  79.                 buyButton.click()  
  80.                 print (user+‘立即申購’)  
  81.                 break
  82.             except :      
  83.                 if no_found_bug:  
  84.                     time.sleep(0.3)  
  85.                     if BUY_URL == driver.current_url :#還在當前頁面自動重新整理
  86.                         driver.get(BUY_URL)  
  87.                         pass
  88.                     else:  
  89.                         print(user+‘手動點選了申購’)  
  90.                         break
  91.                 else:  
  92.                     print (user+‘點選不了申購!!!!!!需要手動點選!!!!!’)  
  93.                     time.sleep(0.5)  
  94.                     if BUY_URL != driver.current_url :  
  95.                         print(user+‘手動點選了申購’)  
  96.                         break
  97.                     pass
  98.         else:  
  99.             time.sleep(15)  
  100.             print (user+‘睡眠15s,未到指令碼開啟時間:’+BEGIN_GO)  
  101.     onQueue(driver,user)  
  102. #登入商城,登陸成功後手動先訪問baidu頁面跳轉至搶購頁面
  103. def loginMall(user,pwd):  
  104.     driver = webdriver.Chrome()  
  105.     driver.get(LOGIN_URL)  
  106.     hasLogin = False
  107.     try:  
  108.         account = driver.find_element_by_xpath(’//*[@id=”login_userName”]’)  
  109.         account.click()  
  110.         account.send_keys(user)  
  111.         time.sleep(1)  
  112.         password = driver.find_element_by_xpath(’//*[@id=”login_password”]’)  
  113.         password.click()  
  114.         password.send_keys(pwd)  
  115.         print (user+‘輸入了賬號密碼,等待手動登入’)  
  116.     except :      
  117.         print (user+‘賬號密碼不能輸入’)  
  118.     whileTrue:  
  119.             time.sleep(3)  
  120.             if LOGIN_SUCCESS_CONFIRM == driver.current_url :  
  121.                 print(user+‘登入成功!’)  
  122.                 break
  123.     goToBuy(driver,user)  
  124. if __name__ == “__main__”:  
  125.     # 賬號密碼
  126.     data = ACCOUNTS  
  127.     # 構建執行緒 
  128.     threads = []    
  129.     for account, pwd in data.items():    
  130.        t = Thread(target=loginMall,args=(account,pwd,))  
  131.        threads.append(t)    
  132.     # 啟動所有執行緒
  133.     for thr in threads:  
  134.         time.sleep(2)  
  135.         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>