selenium+python自动抢购源码

  1.   

    华为MATE10抢购源码,使用selenium+python+Chrome

    大概流程:

    1、按帐号生成多个线程开启浏览器

    2、登陆帐号密码,有时需要验证码,此处有手动操作

    3、不停刷新抢购页面,抢购按钮出现时选择规格提交订单

    4、排队页面等待,直到不在排队

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

    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.   
    7.   
    8. ACCOUNTS = {  
    9.         ”account1”:“password1”  
    10.         ”account2”:“password2”  
    11.     }  
    12.   
    13. #MATE10 抢购地址  
    14. #BUY_URL = ‘https://www.vmall.com/product/396602535.html’  
    15. #保时捷  
    16. BUY_URL = ’https://www.vmall.com/product/173840389.html’  
    17. #测试  
    18. # BUY_URL = ‘https://www.vmall.com/product/144380118.html’  
    19. #登录url  
    20. 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’  
    21. #登录成功手动确认URL  
    22. LOGIN_SUCCESS_CONFIRM = ’https://www.baidu.com/’  
    23. #开始自动刷新等待抢购按钮出现的时间点,提前3分钟  
    24. BEGIN_GO = ’2017-11-16 10:07:10’  
    25.   
    26.   
    27.   
    28. #进到购买页面后提交订单  
    29. def submitOrder(driver,user):  
    30.     time.sleep(1)  
    31.     while BUY_URL == driver.current_url :  
    32.         print (user+‘:当前页面还在商品详情!!!’)  
    33.         time.sleep(3)  
    34.   
    35.     while True:  
    36.         try:  
    37.             submitOrder = driver.find_element_by_link_text(’提交订单’)  
    38.             submitOrder.click()  
    39.             print (user+‘:成功提交订单’)  
    40.             break  
    41.         except :  
    42.             print (user+‘:提交不了订单!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!’)  
    43.             time.sleep(1)#到了订单提交页面提交不了订单一直等待   
    44.             pass  
    45.     while True:  
    46.         time.sleep(3000)  
    47.         print (user+‘:进入睡眠3000s’)  
    48.         pass  
    49.   
    50. #排队中  
    51. def onQueue(driver,user):  
    52.     time.sleep(1)  
    53.     nowUrl = driver.current_url  
    54.     while True:  
    55.         try:  
    56.             tryAgain = driver.find_element_by_link_text(’再试一次’)  
    57.             tryAgain.click()  
    58.             print (user+‘:再试一次点击’)  
    59.             pass  
    60.         except :  
    61.             print (user+‘:排队中’)  
    62.             time.sleep(0.3)#排队中  
    63.             pass  
    64.         if nowUrl != driver.current_url:  
    65.             print (user+‘:排队页面跳转了!!!!!!!!!!!!!!’)  
    66.             break  
    67.     submitOrder(driver,user)  
    68.       
    69. #登录成功去到购买页面  
    70. def goToBuy(driver,user):  
    71.     driver.get(BUY_URL)  
    72.     print (user+‘打开购买页面’)  
    73.     #转换成抢购时间戳  
    74.     timeArray = time.strptime(BEGIN_GO, ”%Y-%m-%d %H:%M:%S”)  
    75.     timestamp = time.mktime(timeArray)  
    76.     #未发现购买按钮  
    77.     no_found_bug = True  
    78.     while True:  
    79.         if time.time()>timestamp:#到了抢购时间  
    80.             try:  
    81.                 buyButton = driver.find_element_by_link_text(’立即申购’)  
    82.                 no_found_bug = False  
    83.                 print (user+‘立即申购按钮出现了!!!’)  
    84.                 #点击摩卡金  
    85.                 # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[1]/div/ul/li[2]/div/a/p/span’).click()  
    86.                 #点击6gb+128gb  
    87.                 # driver.find_element_by_xpath(‘//*[@id=”pro-skus”]/dl[3]/div/ul/li[2]/div/a/p/span’).click()  
    88.                 buyButton.click()  
    89.                 print (user+‘立即申购’)  
    90.                 break  
    91.             except :      
    92.                 if no_found_bug:  
    93.                     time.sleep(0.3)  
    94.                     if BUY_URL == driver.current_url :#还在当前页面自动刷新  
    95.                         driver.get(BUY_URL)  
    96.                         pass  
    97.                     else:  
    98.                         print(user+‘手动点击了申购’)  
    99.                         break  
    100.                 else:  
    101.                     print (user+‘点击不了申购!!!!!!需要手动点击!!!!!’)  
    102.                     time.sleep(0.5)  
    103.                     if BUY_URL != driver.current_url :  
    104.                         print(user+‘手动点击了申购’)  
    105.                         break  
    106.                     pass  
    107.         else:  
    108.             time.sleep(15)  
    109.             print (user+‘睡眠15s,未到脚本开启时间:’+BEGIN_GO)  
    110.     onQueue(driver,user)  
    111.   
    112. #登录商城,登陆成功后手动先访问baidu页面跳转至抢购页面  
    113. def loginMall(user,pwd):  
    114.     driver = webdriver.Chrome()  
    115.     driver.get(LOGIN_URL)  
    116.     hasLogin = False  
    117.     try:  
    118.         account = driver.find_element_by_xpath(’//*[@id=”login_userName”]’)  
    119.         account.click()  
    120.         account.send_keys(user)  
    121.         time.sleep(1)  
    122.         password = driver.find_element_by_xpath(’//*[@id=”login_password”]’)  
    123.         password.click()  
    124.         password.send_keys(pwd)  
    125.         print (user+‘输入了账号密码,等待手动登录’)  
    126.     except :      
    127.         print (user+‘账号密码不能输入’)  
    128.   
    129.     while True:  
    130.             time.sleep(3)  
    131.             if LOGIN_SUCCESS_CONFIRM == driver.current_url :  
    132.                 print(user+‘登录成功!’)  
    133.                 break  
    134.     goToBuy(driver,user)  
    135.   
    136.   
    137.   
    138.   
    139.   
    140.   
    141. if __name__ == “__main__”:  
    142.     # 账号密码  
    143.     data = ACCOUNTS  
    144.     # 构建线程   
    145.     threads = []    
    146.     for account, pwd in data.items():    
    147.        t = Thread(target=loginMall,args=(account,pwd,))  
    148.        threads.append(t)    
    149.     # 启动所有线程  
    150.     for thr in threads:  
    151.         time.sleep(2)  
    152.         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 抢购地址

    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)

    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()

    转载自:http://blog.csdn.net/pengsidong/article/details/79207659

    展开阅读全文

    没有更多推荐了,返回首页