某东天天做活动,今天纸品清洁超级品类日,14点抢一个199-198瞬间没了。我就想是不是可以用脚本来抢,于是记录下过程。

准备

  • python
  • chrome浏览器
  • chromedriver
  • selenium

python + pip 环境安装请参考:
http://www.cnblogs.com/duoyi/articles/7904634.html

安装selenium

selenium是一个功能自动化工具,官网:https://www.seleniumhq.org(需要梯子)
我们可以用pip来安装selenium

1
$ pip install selenium

下载chromedriver

使用webDriver再chrome上测试时,需要下载chrome驱动,从http://chromedriver.storage.googleapis.com/index.html下载
下载完成后注意要把chromedriver放到python文件夹(如 C:\Users\Administrator\AppData\Local\Programs\Python\Python36)和chrome文件夹(如 C:\Program Files\Google\Chrome\Application)下面

编写代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#coding:utf-8
from selenium import webdriver
import time
import datetime
class JDQUAN(object):
def __init__(self,url):
self.driver=webdriver.Chrome()
self.driver.get(url)
def login_jd(self,num,pwd):
self.driver.find_element_by_link_text('账户登录').click()
self.driver.find_element_by_id('loginname').send_keys(num)
self.driver.find_element_by_id('nloginpwd').send_keys(pwd)
self.driver.find_element_by_id('loginsubmit').click()
def buy_on_time(self,buytime,quan_link):
while True:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if now == buytime:
while True:
self.driver.get(quan_link)
time.sleep(0.1)
time.sleep(0.5)
def start(self,buytime,quan_link):
num='num'
pwd='pwd'
self.login_jd(num,pwd)
self.buy_on_time(buytime,quan_link)
jd=JDQUAN('https://passport.jd.com/new/login.aspx')
jd.start('2018-08-28 20:00:00','https://coupon.jd.com/ilink/couponSendFront/send_index.action?key=a53659ef12e44caaa8fca46ff86a0bbe&roleId=13710707&to=sale.jd.com/act/s6qSug4WUfYZKkzl.html&cpdad=1DLSUE')

num和pwd为账号和比吗,实例化JDQUAN穿入的url是登录页,start函数的两个参数是开始的时间和优惠券的地址。
优惠券的地址可以按f12再浏览器里找到。

如果登录的时候遇到需要输入验证码的情况,可以手动输入,再手动点击登录。只要在抢券时间前登录就可以。
最后直接运行就可以了,运气好的话就能抢到了。

1
$ python main.py