手把手教你用python搶京東大額神券

一、背景介紹



我們經常能看到京東上有滿200減100之類的大額神券,但是當自己激動的等在電腦旁,手拿鼠標、眼盯屏幕,等著倒計時慢慢臨近,期待搶到自己喜愛的優惠券,然而最終得到的往往是“券已搶完”

手把手教你用python搶京東大額神券

為了彌補手速的缺憾,今天我教大家如何用python搶優惠券,讓你的“手速”提高數十倍。


二、抓包分析

首先,我用瀏覽器抓包發現,搶券過程是通過get請求實現的:

手把手教你用python搶京東大額神券

請求參數為:

手把手教你用python搶京東大額神券

主要就是key值,它是每個優惠券的標誌參數,我們有3種方法可以找到它:

  1. 在原網頁中:
手把手教你用python搶京東大額神券

data-key的值即為上述的key值,我們可以用selenium從原網頁獲取key值,這種方法只能用selenium操控瀏覽器獲得key值。

2.抓包獲取傳遞key值的數據接口:


手把手教你用python搶京東大額神券


接口地址:

<code>https://a.jd.com/indexAjax/getCouponListByCatalogId.html?&catalogId=134&page=1&pageSize=9&_=1588425128493/<code>

每頁返回9個優惠券信息,page代表頁碼,pageSize代表返回數量。

三、python模擬請求

Key值找到了,下面就可以用requests模擬搶券了,python模擬請求代碼如下:


<code>/<code>
<code>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)/<code>
<code>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)/<code>

我把代碼分成了兩部分,領券是由第二段代碼實現的,那麼為什麼要用第一段呢?

其實這是一種反爬措施,就是領券之前要先進行驗證,讓服務器知道這是哪個用戶在領券,在之前的文章中有詳細介紹:細說小白學python爬蟲過程中常見的反爬措施及解決思路(乾貨)



第一段代碼的返回結果為:


<code>{"Identity":{"Unick":"小笨鳥","Name":"jd_100000000","IsAuthenticated":true}}/<code>


第二段代碼的返回結果為:

<code>jQuery708242({"code":"999","success":true,"message":"領券成功"})/<code>

將這兩段代碼合成一塊就是這次搶大額優惠券的代碼了。


以上介紹的只是如何用python實現普通優惠券的領券過程,但我們要搶的券都是有倒計時的,所以,要加循環並檢測的功能,並在倒計時快結束了的時候循環運行程序:

<code>while True:    response2 = requests.get(url2, headers=header)    response2.encoding='utf-8'    if '領券成功' in response2.text:        break/<code>


最後,展示一下批量領取優惠券的過程,代碼如下:

<code>#批量領取優惠券代碼# 公眾號【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)​/<code>


除了不能領的,都顯示領取成功了:

手把手教你用python搶京東大額神券

學會這項技能,再也不怕搶不到券了!

本文所述的代碼已上傳至【python的爬蟲與數據分析之路】後臺,請輸入優惠券獲取。另外,程序中出現的問題可以私聊我:


分享到:


相關文章: