python抢小米6自动化脚本

1、环境

    windows+python27+selenium

2、代码

#!/usr/bin/env python
# coding=utf-8

"""
              ┏┓    ┏┓
            ┏━┛┻━━━━┛┻━┓
            ┃     ☃    ┃
            ┃   ┳┛ ┗┳  ┃
            ┃     ┻    ┃
            ┗━┓      ┏━┛
              ┃      ┗━━━┓
              ┃  神兽保佑 ┣┓
              ┃ 永无BUG!┏┛
              ┗┓┓┏━━━━┳┓┏┛
               ┃┫┫    ┃┫┫
               ┗┻┛    ┗┻┛
"""

from selenium import webdriver
import time


class GetXiaoMi:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.url = 'https://item.mi.com/product/10000041.html'

    def login(self):
        try:
            self.driver.get(self.url)
            time.sleep(2)
            home_login_butten = self.driver.find_element_by_xpath(".//*[@id='J_buyBox']/div/div[1]/div/a[1]")
            home_login_butten.click()
            input_username = self.driver.find_element_by_class_name('item_account')
            input_username.clear()
            input_username.send_keys('username')
            input_password = self.driver.find_element_by_id('pwd')
            input_password.clear()
            input_password.send_keys('password')
            login_butten = self.driver.find_element_by_id('login-button')
            login_butten.click()
            assert (self.driver.find_element_by_xpath(".//*[@id='error-outcon']/div/span]"),
                    u"登陆失败,请检查用户名或密码")
        except Exception, e:
            print e

    @staticmethod
    def get_sys_time():
        sys_time = time.time()
        return sys_time

    @staticmethod
    def set_stamp():
        set_time = '2017-05-12 09:59:55'  # 设置抢购时间,最好提前几秒
        # 将其转换为时间数组
        time_array = time.strptime(set_time, '%Y-%m-%d %H:%M:%S')
        # 转换为时间戳
        time_stamp = int(time.mktime(time_array))
        return time_stamp

    def get_xiaomi(self):
        try:
            if self.get_sys_time() >= self.set_stamp():
                while True:
                    self.driver.find_element_by_class_name('btn btn-primary btn-biglarge J_proBuyBtn add').click()
                    if self.driver.title == u'你来晚了':
                        print u'又悲剧了,默默的问候小米~'
                        break
                    else:
                        print u'<-------------赶紧手动付款吧------------>'
            else:
                print u'时间设置错误'
        except Exception, e:
            print e


if __name__ == '__main__':
    run = GetXiaoMi()
    run.get_xiaomi()

代码报错?你是否安装了selenium模块?请自行百度~

缺少chromedriver?那就下载一个~

代码还报错?找不到元素?小米官网的按钮元素会经常更换名字的,这就得自己搞定了~

PS:最后我想说的是,脚本调试中,不一定好用,没有调接口来的痛快,哎,屌丝的人生就是这样的,干什么都得靠抢。

转载于:https://my.oschina.net/medivhxu/blog/898458

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值