背景
- 在線上買了個 iphone13pro,可是等得我好着急呀!一直不發貨..甚至關注了 iphone13 超話每天看看大家下的訂單都發貨了沒...
- 於是乎還看到了有人預定了線下門店的提貨,害得我也想直接去線下店提貨了..但是每次打開官網都顯示【暫無供應】
- 於是乎想到了簡單的爬蟲(其實就是發個請求而已...),然後做成定時任務,當有貨的時候第一時間通知我
完成步驟
- 打開官網,找到獲取線下門店供應情況的接口
- 抓包接口,猜測哪個字段影響供貨顯示情況
- 改包接口,將該字段改成有貨,驗證猜測
- Python 請求該接口,取到該字段值
- 接入釘釘機器人,將廣州線下門店的供貨情況發送到釘釘上
- 使用 mac 自帶的 crontab 定時任務,創建執行 Python 腳本的定時任務
找接口
https://www.apple.com.cn/shop/fulfillment-messages?pl=true&parts.0=MLTE3CH/A&location=%E5%B9%BF%E4%B8%9C%20%E5%B9%BF%E5%B7%9E%20%E5%A4%A9%E6%B2%B3%E5%8C%BA
很容易找到供貨情況的接口是這個
找字段
盲猜是這個 storeSelectionEnabled 字段影響的,因爲只有它是 False,當然也有猜過是下面的 pickupDisplay,但從命名來看,第一個纔是影響是否可選中的字段
驗證字段
- 通過 charles 的 map local 來改包
- 先保存這個接口的響應體到本地,然後將 storeSelectionEnabled 改成 true
- 再按照下面的步驟來綁定響應映射就可以了
- 再次請求下網站,可以看到是可選中了
- 當然,我其實並不知道真正有供貨的響應體是怎麼樣的,因爲找了好幾個城市都是無貨狀態,無法直接複製
- 所以只能假設可選中就是有供貨,準確率估計 50%?哈哈
Python 腳本請求該接口
import requests def get_phone(): res = requests.get( "https://www.apple.com.cn/shop/fulfillment-messages?pl=true&parts.0=MLTE3CH/A&location=%E5%B9%BF%E4%B8%9C%20%E5%B9%BF%E5%B7%9E%20%E5%A4%A9%E6%B2%B3%E5%8C%BA", verify=False) res = res.json()["body"]["content"]["pickupMessage"]["stores"] for num, item in enumerate(res): phone = item["partsAvailability"]["MLTE3CH/A"] storeSelectionEnabled = phone["storeSelectionEnabled"] storePickupQuote = phone["storePickupQuote"] pickupSearchQuote = phone["pickupSearchQuote"] if storeSelectionEnabled: res = { "可取貨": storeSelectionEnabled, "取貨狀態": storePickupQuote, "供應狀態": pickupSearchQuote } yield res
接入釘釘機器人
https://www.cnblogs.com/poloyy/p/15565764.html
最終腳本
import requests # 獲取手機供貨信息 def get_phone(): res = requests.get( "https://www.apple.com.cn/shop/fulfillment-messages?pl=true&parts.0=MLTE3CH/A&location=%E5%B9%BF%E4%B8%9C%20%E5%B9%BF%E5%B7%9E%20%E5%A4%A9%E6%B2%B3%E5%8C%BA", verify=False) res = res.json()["body"]["content"]["pickupMessage"]["stores"] for num, item in enumerate(res): phone = item["partsAvailability"]["MLTE3CH/A"] storeSelectionEnabled = phone["storeSelectionEnabled"] storePickupQuote = phone["storePickupQuote"] pickupSearchQuote = phone["pickupSearchQuote"] if storeSelectionEnabled: res = { "可取貨": storeSelectionEnabled, "取貨狀態": storePickupQuote, "供應狀態": pickupSearchQuote } yield res # python 3.8 import time import hmac import hashlib import base64 import urllib.parse # 加簽 timestamp = str(round(time.time() * 1000)) secret = '此處填寫 webhook token' secret_enc = secret.encode('utf-8') string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) def dingmessage(): # 請求的URL,WebHook地址 webhook = f"https://oapi.dingtalk.com/robot/send?access_token={token}×tamp={timestamp}&sign={sign}" # 構建請求頭部 header = {"Content-Type": "application/json", "Charset": "UTF-8"} # 循環生成器併發送消息 for phone in get_phone(): message = { "msgtype": "text", "text": {"content": phone}, "at": { # @ 所有人 "isAtAll": True } } message_json = json.dumps(message) info = requests.post(url=webhook, data=message_json, headers=header, verify=False) # 打印返回的結果 print(info.text) if __name__ == "__main__": dingmessage()
運行一下
接入 crontab 定時任務
我的是 mac 電腦,所有有自帶 crontab,命令行敲
sudo crontab -e
添加定時任務
*/1 * * * * /usr/local/opt/python@3.9/bin/python3.9 /Users/test.py
保存,會有成功信息
> sudo crontab -e crontab: installing new crontab
查看定時任務
sudo crontab -l
過一分鐘後去看釘釘就可以了!
特殊情況
若遇到定時任務無法運行,可以看看這篇
https://www.cnblogs.com/poloyy/p/15565916.html