一、背景介绍
我们经常能看到京东上有满200减100之类的大额神券,但是当自己激动的等在电脑旁,手拿鼠标、眼盯屏幕,等着倒计时慢慢临近,期待抢到自己喜爱的优惠券,然而最终得到的往往是“券已抢完”
![7bc1c31a156ff740185aef7020838554.png](https://i-blog.csdnimg.cn/blog_migrate/398014e8ff600b8efcd81b9fa5e7a074.jpeg)
为了弥补手速的缺憾,今天我教大家如何用python抢优惠券,让你的“手速”提高数十倍。
二、抓包分析
首先,我用浏览器抓包发现,抢券过程是通过get请求实现的:
![1a6ca0103df95e9646f556cdea1fcc1b.png](https://i-blog.csdnimg.cn/blog_migrate/1db35a3b5d523d0a043bc2f0759e91d3.jpeg)
请求参数为:
![d484968b7048f8f432ee87e2d15c9934.png](https://i-blog.csdnimg.cn/blog_migrate/29ae3b5f163f27c328916123a72f96a2.jpeg)
主要就是key值,它是每个优惠券的标志参数,我们有3种方法可以找到它:
- 在原网页中:
![eab574d9f319e5a438067ee293681462.png](https://i-blog.csdnimg.cn/blog_migrate/4c348eb4953091483592173205344b68.jpeg)
data-key的值即为上述的key值,我们可以用selenium从原网页获取key值,这种方法只能用selenium操控浏览器获得key值。
2.抓包获取传递key值的数据接口:
![3f442d4d8af3422eba19de8cdbb19468.png](https://i-blog.csdnimg.cn/blog_migrate/32f6113350ec6cef3462f0f3a09a87b4.jpeg)
接口地址:
https://a.jd.com/indexAjax/getCouponListByCatalogId.html?&catalogId=134&page=1&pageSize=9&_=1588425128493
每页返回9个优惠券信息,page代表页码,pageSize代表返回数量。
三、python模拟请求
Key值找到了,下面就可以用requests模拟抢券了,python模拟请求代码如下:
import requestsheader={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0?', 'Referer':'https://a.jd.com/', 'Cookie': '写入你的cookie'}key = '0271DFD6890D3B60ACB8BA8A9E49BEB17FE8E6323A36834B63FE69E95D38088EFCFCC41CEDA463F657AC10A29B05CD8C80317528B28221B996820855DF2962B519E4BCD69F2FAD2A6A0C71D95C08EC37657F57BE5FF35205CD6EB7B98375482F'session = requests.session()url1 = 'https://passport.jd.com/loginservice.aspx?&method=Login&_=1588432511753'response1 = requests.get(url1, headers=header)
url2 = 'https://a.jd.com/indexAjax/getCoupon.html?callback=jQuery708242&key={0}&type=1&_=1588433465288'.format(key)header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0?', 'Referer':'https://a.jd.com/', 'Cookie': '写入你的cookie'}response2 = requests.get(url2, headers=header)
我把代码分成了两部分,领券是由第二段代码实现的,那么为什么要用第一段呢?
其实这是一种反爬措施,就是领券之前要先进行验证,让服务器知道这是哪个用户在领券,在之前的文章中有详细介绍:细说小白学python爬虫过程中常见的反爬措施及解决思路(干货)
第一段代码的返回结果为:
{"Identity":{"Unick":"小笨鸟","Name":"jd_100000000","IsAuthenticated":true}}
第二段代码的返回结果为:
jQuery708242({"code":"999","success":true,"message":"领券成功"})
将这两段代码合成一块就是这次抢大额优惠券的代码了。
以上介绍的只是如何用python实现普通优惠券的领券过程,但我们要抢的券都是有倒计时的,所以,要加循环并检测的功能,并在倒计时快结束了的时候循环运行程序:
while True: response2 = requests.get(url2, headers=header) response2.encoding='utf-8' if '领券成功' in response2.text: break
最后,展示一下批量领取优惠券的过程,代码如下:
#批量领取优惠券代码# 公众号【python的爬虫与数据分析之路】import requestsimport jsonheader={ 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0', 'Referer':'https://a.jd.com/'}cookie={ 'Cookie':'写入你的cookie'}for i in range(3):#获得key值 url='https://a.jd.com/indexAjax/getCouponListByCatalogId.html?callback=jQuery8020514&catalogId=134&page={0}&pageSize=9&_=1588487277055'.format(i+1) response = requests.get(url, headers=header) items=json.loads(response.text[14:].replace(')', '')) for key in items['couponList']: key=key['ruleKey'] header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0?', 'Referer': 'https://a.jd.com/', 'Cookie': '写入你的cookie'} session = requests.session() url1 = 'https://passport.jd.com/loginservice.aspx?&method=Login&_=1588432511753' response1 = requests.get(url1, headers=header) url2 = 'https://a.jd.com/indexAjax/getCoupon.html?callback=jQuery708242&key={0}&type=1&_=1588433465288'.format( key) header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0?', 'Referer': 'https://a.jd.com/', 'Cookie': '写入你的cookie'} response2 = requests.get(url2, headers=header) response2.encoding = 'utf-8' print(response2.text)
除了不能领的,都显示领取成功了:
![1a5417670f74c3a88b70806177d7dafb.png](https://i-blog.csdnimg.cn/blog_migrate/7b91147211b3d458daf3ff688a1a5c42.jpeg)
学会这项技能,再也不怕抢不到券了!
本文所述的代码已上传至【python的爬虫与数据分析之路】后台,请输入优惠券获取。另外,程序中出现的问题可以私聊我: