selenium+python 京东自动登录及秒杀

运行环境:

  • python 2.7
  • python安装selenium
  • 安装webdriver(这里是firefox)

其中selenium可以采用pip安装:

pip install selenium1

webdriver下载地址 

需要注意的是,webdriver的目录、对应浏览器的目录,都要添加到path。

代码如下:

x# _*_coding:utf-8_*_#Python 自动抢单脚本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 = '2017-09-11 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')12345678910111213141516171819202122232425262728293031323334353637383940

使用方法: 

要秒杀的东西要首先添加在购物车中,且购物车只有这一件商品!!! 配置好环境后,在程序入口处login函数填上自己的京东用户名和密码,在buy_on_time函数处设置秒杀时间,然后运行程序即可。要注意秒杀时间格式,并确保自己电脑时钟准确。

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s