运行环境
python需要安装selenium
pip install selenium
安装 webdriver
下载chromedriver
http://chromedriver.storage.googleapis.com/index.html
2.29是chrome最新版,Windows下下载对应版本放到\Google\Chrome\Application\chromedriver.exe,同时添加到系统环境变量
脚本
#-*- coding:utf-8 -*-
#京东抢手机脚本
from selenium import webdriver
import datetime
import time
driver = webdriver.Chrome(executable_path="C:\Users\ZouJinyi\AppData\Local\Google\Chrome\Application\chromedriver.exe")
def login(uname, pwd):
driver.get("http://www.jd.com")
driver.find_element_by_link_text("你好,请登录").click()
time.sleep(0.1)
driver.find_element_by_link_text("账户登录").click()
driver.find_element_by_name("loginname").send_keys(uname)
driver.find_element_by_name("nloginpwd").send_keys(pwd)
driver.find_element_by_id("loginsubmit").click()
time.sleep(0.1)
driver.get("https://cart.jd.com/cart.action")
time.sleep(0.1)
driver.find_element_by_partial_link_text("去结算").click()
now = datetime.datetime.now()
print now.strftime('%Y-%m-%d %H:%M:%S')
print 'login success'
def buy_on_time(buytime):
while True:
now = datetime.datetime.now()
if now.strftime('%Y-%m-%d %H:%M:%S') == buytime:
driver.find_element_by_id('order-submit').click()
time.sleep(0.1)
print now.strftime('%Y-%m-%d %H:%M:%S')
print 'purchase success'
time.sleep(0.1)
# entrance
login('usr', 'pwd')
buy_on_time('2018-02-26 10:00:00')