├── README.md ├── seckill.py ├── .gitignore └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # secKill 2 | ##小米商城秒杀脚本 3 | 4 | 2018/09/21 01:20 5 | 针对小米商城网页版秒杀活动开发的脚本 6 | 7 | 环境相关: 8 | * python3.6 9 | * selenium,浏览器自动化测试框架 10 | * chrome 11 | -------------------------------------------------------------------------------- /seckill.py: -------------------------------------------------------------------------------- 1 | 2 | #网上参考原版 3 | 4 | from selenium import webdriver 5 | import time 6 | import datetime 7 | 8 | browser = webdriver.Chrome("D:\softInstall\chromedriver.exe") 9 | 10 | def login(name ,pwd): 11 | browser.get( 'https://account.xiaomi.com/')#登录网址 12 | time.sleep(2) 13 | browser.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 14 | browser.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 15 | browser.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 16 | #如果找不到标签ID,可以使用其他方法来确定元素位置 17 | time.sleep(3) 18 | 19 | print('登录成功,正在等待秒杀···') 20 | 21 | def buy_on_time(buytime): 22 | browser.get("https://www.mi.com/seckill/")#切换到秒杀页面 23 | while True: #不断刷新时钟 24 | now = datetime.datetime.now() 25 | # if now.strftime('%Y-%m-%d %H:%M:%S') == buytime: 26 | browser.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[6]/div[2]/a[2]').click() #购买按钮的Xpath 27 | browser.find_element_by_xpath('/html/body').click() #取消提醒 28 | print('下单成功,请抓紧付款!') 29 | 30 | time.sleep(0.6)#注意刷新间隔时间要尽量短 31 | 32 | 33 | #配置账号密码 34 | # login('username' , 'password') 35 | buy_on_time('2018-06-07 21:25:00')#指定秒杀时间,并且开始等待秒杀 36 | 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | from selenium import webdriver 4 | from selenium.common.exceptions import NoSuchElementException 5 | import time 6 | import datetime 7 | import threading 8 | 9 | #时钟间隔 10 | ti=0.001 11 | #秒杀时间 12 | buytime="2018-09-05 23:59:00" 13 | 14 | #配置账号密码 15 | name="username" 16 | pwd="password" 17 | 18 | #创建8个浏览器 19 | browser1= webdriver.Chrome("D:\softInstall\chromedriver.exe") 20 | browser2= webdriver.Chrome("D:\softInstall\chromedriver.exe") 21 | browser3= webdriver.Chrome("D:\softInstall\chromedriver.exe") 22 | browser4= webdriver.Chrome("D:\softInstall\chromedriver.exe") 23 | browser5= webdriver.Chrome("D:\softInstall\chromedriver.exe") 24 | browser6= webdriver.Chrome("D:\softInstall\chromedriver.exe") 25 | browser7= webdriver.Chrome("D:\softInstall\chromedriver.exe") 26 | browser8= webdriver.Chrome("D:\softInstall\chromedriver.exe") 27 | 28 | 29 | #创建8个方法 30 | def bro1(): 31 | 32 | browser1.get( 'https://account.xiaomi.com')#登录网址 33 | time.sleep(2) 34 | browser1.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 35 | browser1.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 36 | browser1.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 37 | #如果找不到标签ID,可以使用其他方法来确定元素位置 38 | time.sleep(3) 39 | 40 | browser1.get("https://www.mi.com/seckill/") 41 | 42 | while True: #不断刷新时钟 43 | now = datetime.datetime.now() 44 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 45 | while True: 46 | try : 47 | browser1.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[1]/div[2]/a[1]').click() #购买按钮的Xpath 48 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 49 | except NoSuchElementException: 50 | print("元素异常") 51 | print('下单成功,请抓紧付款!1') 52 | 53 | time.sleep(ti)#注意刷新间隔时间要尽量短 54 | def bro2(): 55 | 56 | browser2.get( 'https://account.xiaomi.com')#登录网址 57 | time.sleep(2) 58 | browser2.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 59 | browser2.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 60 | browser2.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 61 | #如果找不到标签ID,可以使用其他方法来确定元素位置 62 | time.sleep(3) 63 | 64 | browser2.get("https://www.mi.com/seckill/") 65 | 66 | while True: #不断刷新时钟 67 | now = datetime.datetime.now() 68 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 69 | while True: 70 | try : 71 | browser2.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[1]/div[2]/a[2]').click() #购买按钮的Xpath 72 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 73 | except NoSuchElementException: 74 | print("元素异常") 75 | print('下单成功,请抓紧付款!2') 76 | 77 | time.sleep(ti)#注意刷新间隔时间要尽量短 78 | def bro3(): 79 | 80 | browser3.get( 'https://account.xiaomi.com')#登录网址 81 | time.sleep(2) 82 | browser3.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 83 | browser3.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 84 | browser3.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 85 | #如果找不到标签ID,可以使用其他方法来确定元素位置 86 | time.sleep(3) 87 | 88 | 89 | browser3.get("https://www.mi.com/seckill/") 90 | while True: #不断刷新时钟 91 | now = datetime.datetime.now() 92 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 93 | while True: 94 | try : 95 | browser3.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[1]/div[2]/a[3]').click() #购买按钮的Xpath 96 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 97 | except NoSuchElementException: 98 | print("元素异常") 99 | print('下单成功,请抓紧付款!2') 100 | 101 | time.sleep(ti)#注意刷新间隔时间要尽量短 102 | def bro4(): 103 | 104 | browser4.get( 'https://account.xiaomi.com')#登录网址 105 | time.sleep(2) 106 | browser4.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 107 | browser4.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 108 | browser4.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 109 | #如果找不到标签ID,可以使用其他方法来确定元素位置 110 | time.sleep(3) 111 | 112 | browser4.get("https://www.mi.com/seckill/") 113 | while True: #不断刷新时钟 114 | now = datetime.datetime.now() 115 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 116 | while True: 117 | try : 118 | browser4.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[1]/div[2]/a[4]').click() #购买按钮的Xpath 119 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 120 | except NoSuchElementException: 121 | print("元素异常") 122 | print('下单成功,请抓紧付款!2') 123 | 124 | time.sleep(ti)#注意刷新间隔时间要尽量短 125 | def bro5(): 126 | 127 | browser5.get( 'https://account.xiaomi.com')#登录网址 128 | time.sleep(2) 129 | browser5.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 130 | browser5.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 131 | browser5.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 132 | #如果找不到标签ID,可以使用其他方法来确定元素位置 133 | time.sleep(3) 134 | browser5.get("https://www.mi.com/seckill/") 135 | while True: #不断刷新时钟 136 | now = datetime.datetime.now() 137 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 138 | while True: 139 | try : 140 | browser5.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[3]/div[2]/a[5]').click() #购买按钮的Xpath 141 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 142 | except NoSuchElementException: 143 | print("元素异常") 144 | print('下单成功,请抓紧付款!2') 145 | 146 | time.sleep(ti)#注意刷新间隔时间要尽量短 147 | def bro6(): 148 | browser6.get( 'https://account.xiaomi.com')#登录网址 149 | time.sleep(2) 150 | browser6.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 151 | browser6.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 152 | browser6.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 153 | #如果找不到标签ID,可以使用其他方法来确定元素位置 154 | time.sleep(3) 155 | 156 | browser6.get("https://www.mi.com/seckill/") 157 | while True: #不断刷新时钟 158 | now = datetime.datetime.now() 159 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 160 | while True: 161 | try : 162 | browser6.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[3]/div[2]/a[6]').click() #购买按钮的Xpath 163 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 164 | except NoSuchElementException: 165 | print("元素异常") 166 | print('下单成功,请抓紧付款!2') 167 | 168 | time.sleep(ti)#注意刷新间隔时间要尽量短 169 | def bro7(): 170 | 171 | browser7.get( 'https://account.xiaomi.com')#登录网址 172 | time.sleep(2) 173 | browser7.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 174 | browser7.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 175 | browser7.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 176 | #如果找不到标签ID,可以使用其他方法来确定元素位置 177 | time.sleep(3) 178 | 179 | browser7.get("https://www.mi.com/seckill/") 180 | while True: #不断刷新时钟 181 | now = datetime.datetime.now() 182 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 183 | while True: 184 | try : 185 | browser7.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[3]/div[2]/a[7]').click() #购买按钮的Xpath 186 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 187 | except NoSuchElementException: 188 | print("元素异常") 189 | print('下单成功,请抓紧付款!2') 190 | 191 | time.sleep(ti)#注意刷新间隔时间要尽量短 192 | def bro8(): 193 | 194 | browser8.get( 'https://account.xiaomi.com')#登录网址 195 | time.sleep(2) 196 | browser8.find_element_by_id("username").send_keys(name) #利用账号标签的ID,确定位置并send信息 197 | browser8.find_element_by_id("pwd").send_keys(pwd) #利用密码标签的ID,确定位置并send信息 198 | browser8.find_element_by_id("login-button").click()#利用登录按钮的ID,确定位置并点击 199 | #如果找不到标签ID,可以使用其他方法来确定元素位置 200 | time.sleep(3) 201 | 202 | browser8.get("https://www.mi.com/seckill/") 203 | while True: #不断刷新时钟 204 | now = datetime.datetime.now() 205 | if now.strftime('%Y-%m-%d %H:%M:%S') >= buytime: 206 | while True: 207 | try : 208 | browser8.find_element_by_xpath('/html/body/div[3]/div[2]/div[2]/div/ul[1]/li[3]/div[2]/a[8]').click() #购买按钮的Xpath 209 | # browser1.find_element_by_xpath('/html/body').click() #取消提醒 210 | except NoSuchElementException: 211 | print("元素异常") 212 | print('下单成功,请抓紧付款!8') 213 | 214 | time.sleep(ti)#注意刷新间隔时间要尽量短 215 | 216 | #使用线程 217 | t1=threading.Thread(target=bro1) 218 | t2=threading.Thread(target=bro2) 219 | t3=threading.Thread(target=bro3) 220 | t4=threading.Thread(target=bro4) 221 | t5=threading.Thread(target=bro5) 222 | t6=threading.Thread(target=bro6) 223 | t7=threading.Thread(target=bro7) 224 | t8=threading.Thread(target=bro8) 225 | 226 | t1.start() 227 | t2.start() 228 | t3.start() 229 | t4.start() 230 | t5.start() 231 | t6.start() 232 | t7.start() 233 | t8.start() 234 | 235 | 236 | 237 | 238 | --------------------------------------------------------------------------------