Python作為一款廣泛應用于自動化腳本和網絡爬蟲領域的編程語言,也可以用于進行拼多多的搶購。
import requests import json import time headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} s = requests.session() def login(url): r = s.get(url, headers=headers) if "登錄" in r.text: # 需要登錄,模擬登錄 login_url = "https://apiv4.pinduoduo.com/api/common/login_with_pwd" data = {"password": "xxxxxxxx", "username": "xxxxxxxx"} r = s.post(login_url, headers=headers, data=json.dumps(data)) return r.text def add_cart(goods_id, sku_id, quantity): url = f"https://apiv4.pinduoduo.com/api/cart/add" data = {"group_id": "", "goods_id": goods_id, "sku_id": sku_id, "quantity": quantity} r = s.post(url, headers=headers, data=json.dumps(data)) print(r.text) return r.json()["success"] def buy(goods_id, sku_id, quantity): add_cart(goods_id, sku_id, quantity) time.sleep(1) url = "https://apiv4.pinduoduo.com/api/order/commit" data = {"address_id": "xxxxxxxx", "group_id": "", "goods_info": [ {"goods_id": goods_id, "sku_id": sku_id, "quantity": quantity}], "pay_password": None, "shipment_time_id": ""} r = s.post(url, headers=headers, data=json.dumps(data)) print(r.text) return r.json()["success"] if __name__ == '__main__': login_url = "https://mobile.yangkeduo.com/login.html" login(login_url) add_cart("xxxxxxxx", "xxxxxxxx", 1) buy("xxxxxxxx", "xxxxxxxx", 1)
上述代碼通過HTTP請求的方式,使用Python進行自動化操作,實現了拼多多的搶購功能,并成功將商品添加到購物車和提交訂單。
上一篇mysql動態計算