python实现抢劵_手把手教你用python抢京东大额神券

一、背景介绍


我们经常能看到京东上有满200减100之类的大额神券,但是当自己激动的等在电脑旁,手拿鼠标、眼盯屏幕,等着倒计时慢慢临近,期待抢到自己喜爱的优惠券,然而最终得到的往往是“券已抢完”

7bc1c31a156ff740185aef7020838554.png

为了弥补手速的缺憾,今天我教大家如何用python抢优惠券,让你的“手速”提高数十倍。


二、抓包分析

首先,我用浏览器抓包发现,抢券过程是通过get请求实现的:

1a6ca0103df95e9646f556cdea1fcc1b.png

请求参数为:

d484968b7048f8f432ee87e2d15c9934.png

主要就是key值,它是每个优惠券的标志参数,我们有3种方法可以找到它:

  1. 在原网页中:
eab574d9f319e5a438067ee293681462.png

data-key的值即为上述的key值,我们可以用selenium从原网页获取key值,这种方法只能用selenium操控浏览器获得key值。

2.抓包获取传递key值的数据接口:

3f442d4d8af3422eba19de8cdbb19468.png

接口地址:

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

学会这项技能,再也不怕抢不到券了!

本文所述的代码已上传至【python的爬虫与数据分析之路】后台,请输入优惠券获取。另外,程序中出现的问题可以私聊我:

  • 13
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。关于Python实现面部特效,我可以向你介绍一下Python库Face_recognition,它可以实现人脸识别和面部特效。 Face_recognition是一个基于Python的人脸识别库,它使用dlib库来实现人脸识别,可以检测和识别图像中的人脸,并提取面部特征,比如眼睛、鼻子、嘴巴等部位的位置和轮廓。 具体实现过程如下: 1. 首先需要安装Face_recognition库和dlib库,可以使用pip install face_recognition和pip install dlib命令来安装。 2. 导入Face_recognition库和Pillow库,使用load_image_file函数加载需要处理的图片。 3. 使用face_locations函数来获取图像中人脸的位置坐标,使用face_landmarks函数来获取面部特征的位置坐标。 4. 使用Pillow库的ImageDraw模块来绘制面部特征,比如眼睛、鼻子、嘴巴等部位的位置和轮廓。 下面是一个简单的示例代码,实现在图片中绘制人脸位置和面部特征: ``` import face_recognition from PIL import Image, ImageDraw # 加载图片 image = face_recognition.load_image_file("test.jpg") # 获取人脸位置坐标 face_locations = face_recognition.face_locations(image) # 获取面部特征位置坐标 face_landmarks = face_recognition.face_landmarks(image) # 绘制人脸位置 for face_location in face_locations: top, right, bottom, left = face_location draw = ImageDraw.Draw(image) draw.rectangle(((left, top), (right, bottom)), outline=(255, 0, 0), width=2) # 绘制面部特征 for face_landmark in face_landmarks: for name, points in face_landmark.items(): draw = ImageDraw.Draw(image) draw.line(points, fill=(255, 255, 255), width=2) # 保存绘制后的图片 image.save("result.jpg") ``` 这样就可以实现简单的面部特效了。当然,Face_recognition库还有很多其他的功能和用法,你可以查看官方文档来了解更多。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值