一、運行環境
- 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 函數處設置秒殺時間,然后運行程序即可。要注意秒殺時間格式,並確保自己電腦時鍾准確。
祝您成功!