本文實例為大家分享了selenium+python京東自動登錄及秒殺的代碼,供大家參考,具體內容如下
運行環境:
python 2.7
python安裝selenium
安裝webdriver(這里是firefox)
其中selenium可以采用pip安裝:
pip install selenium
webdriver下載地址
需要注意的是,webdriver的目錄、對應瀏覽器的目錄,都要添加到path。
代碼如下:
# _*_coding:utf-8_*_ from selenium import webdriverimport datetime import timedriver = webdriver.Firefox()def login(uname, pwd): driver.get("http://www.jd.com") driver.find_element_by_link_text("你好,請登錄").click() time.sleep(3) 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(3) driver.get("https://cart.jd.com/cart.action") time.sleep(3) driver.find_element_by_link_text("去結算").click() now = datetime.datetime.now() print now.strftime('%Y-%m-%d %H:%M:%S') print 'login success'# buytime = '2016-12-27 22:31:00' 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)# entrancelogin('username', 'password')buy_on_time('2017-01-01 14:00:00')使用方法:
要秒殺的東西要首先添加在購物車中,且購物車只有這一件商品!!!
配置好環境后,在程序入口處login函數填上自己的京東用戶名和密碼,在buy_on_time函數處設置秒殺時間,然后運行程序即可。要注意秒殺時間格式,并確保自己電腦時鐘準確。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答