一、运行环境
- Windows 7 以上
- Python 3+
- Python 需要安装 selenium
- 安装 webdriver (需要安装 Firefox 浏览器,我的是 57.0 的 )
Firefox 安装略
python + pip 环境搭建请参考另一篇博客:
http://www.cnblogs.com/duoyi/articles/7904634.html
二、通过 pip 安装 selenium
pip install selenium
确保 selenium 的版本为 3.7.0
三、安装 webdriver
1、下载 geckodriver.exe
下载地址:https://github.com/mozilla/geckodriver/releases
请根据您的系统环境选择下载对应版本(如 Windows 64位系统),最好下载比较新的版本。
2、下载解压后将 getckodriver.exe 复制到Firefox的安装目录下,如(C:\Program Files\Mozilla Firefox),并在环境变量Path中添加路径:C:\Program Files\Mozilla Firefox 。
3、重启cmd或IDLE再次运行代码即可。
四、代码
#!/usr/bin/env python
# -*- coding:utf-8 -*- from selenium import webdriver import datetime import time driver = webdriver.Firefox() # http://gate.jd.com/InitCart.aspx?pid=4993737&pcount=1&ptype=1 def login(username, password): driver.get("https://passport.jd.com/new/login.aspx") time.sleep(3) driver.find_element_by_link_text("账户登录").click() driver.find_element_by_name("loginname").send_keys(username) driver.find_element_by_name("nloginpwd").send_keys(password) driver.find_element_by_id("loginsubmit").click() time.sleep(3) driver.get("https://cart.jd.com/cart.action") time.sleep(3) driver.find_element_by_link_text("去结算").click() now = datetime.datetime.now() #now_time = now.strftime('%Y-%m-%d %H:%M:%S') print(now.strftime('%Y-%m-%d %H:%M:%S')) print('login success, you can ou up!')
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(3) print(now.strftime('%Y-%m-%d %H:%M:%S')) print('purchase success') time.sleep(0.5) login('your_jd_username', 'your_jd_password') buy_on_time('2017-11-28 10:00:00')
五、使用说明
要秒杀的东西要首先添加在购物车中,且购物车只有这一件商品! 可以在浏览器访问下面的地址:
http://gate.jd.com/InitCart.aspx?pid=4993737&pcount=1&ptype=1
配置好环境后,在程序入口处login函数填上自己的 京东用户名 和 密码 ,在 buy_on_time 函数处设置秒杀时间,然后运行程序即可。要注意秒杀时间格式,并确保自己电脑时钟准确。
祝您成功!