python+selenium实现京东自动登录及秒杀功能

yipeiwu_com7年前Python基础

本文实例为大家分享了selenium+python京东自动登录及秒杀的代码,供大家参考,具体内容如下

运行环境:

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

其中selenium可以采用pip安装:

pip install selenium

webdriver下载地址

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

代码如下:

# _*_coding:utf-8_*_ 
from selenium import webdriver
import datetime 
import time


driver = 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)


# entrance
login('username', 'password')
buy_on_time('2017-01-01 14:00:00')

使用方法:

要秒杀的东西要首先添加在购物车中,且购物车只有这一件商品!!!

配置好环境后,在程序入口处login函数填上自己的京东用户名和密码,在buy_on_time函数处设置秒杀时间,然后运行程序即可。要注意秒杀时间格式,并确保自己电脑时钟准确。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 比较两个数组的元素的异同方法

通过set()获取两个数组的交/并/差集: print set(a).intersection(set(b)) # 交集 print set(a).union(set(b)) # 并...

Python基础教程之利用期物处理并发

前言 抨击线程的往往是系统程序员,他们考虑的使用场景对一般的应用程序员来说,也许一生都不会遇到……应用程序员遇到的使用场景,99% 的情况下只需知道如何派生一堆独立的线程,然后用队列收集...

浅谈Python traceback的优雅处理

刚接触Python的时候,简单的异常处理已经可以帮助我们解决大多数问题,但是随着逐渐地深入,我们会发现有很多情况下简单的异常处理已经无法解决问题了,如下代码,单纯的打印异常所能提供的信息...

Python 获取命令行参数内容及参数个数的实例

执行python脚本的时候,有时需要获取命令行参数的相关信息。C语言通过argc和argv来获取参数的个数和参数的内容,python中通过sys模块的argv来获取参数的内容,使用len...

Python实现基本数据结构中队列的操作方法示例

Python实现基本数据结构中队列的操作方法示例

本文实例讲述了Python实现基本数据结构中队列的操作方法。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 class Q...