欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python爬虫系列之微信小程序逆向某优选爬虫签名算法

程序员文章站 2022-04-22 11:45:17
代码仅供学习交流,请勿用于非法用途直接上代码import hashlibfrom parser import parseParamfrom sign import sign# 接口秘钥,代码仅供学习,不提供暴露签名secret = ""def md5(s): m = hashlib.md5() m.update(s.encode("utf-8")) return str(m.hexdigest()).lower()def getSign(params....

直接上代码

import hashlib
from parser import parseParam
from sign import sign

# 接口秘钥,代码仅供学习,不提供暴露签名
secret = ""

def md5(s):
    m = hashlib.md5()
    m.update(s.encode("utf-8"))
    return str(m.hexdigest()).lower()


def getSign(params, header):
    paramStr = ""
    if header['content-type'] == "application/x-www-form-urlencoded":
        paramStr = parseParam(params)
    else:
        paramStr = str(params)
    return sign(secret, paramStr, header['api-timestamp'])

if __name__ == '__main__':
    # application/x-www-form-urlencoded 测试
    params = {
        "marketingDataQueryReq": '{"spuSns":["20200823000422101803314"],"areaId":10}',
        "userKey": "请填写自己的userKey",
    }
    header = {
        "content-type": "application/x-www-form-urlencoded",
        "userkey": "请填写自己的userKey",
        "api-version": "V3",
        "api-sign": "46a2a47696572c2c087e5534a311237a",
        "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.15(0x17000f27) NetType/WIFI Language/zh_CN",
        "referer": "https://servicewechat.com/wx6025c5470c3cb50c/170/page-frame.html",
        "api-timestamp": "1598366897123",
    }
    sign = getSign(params, header)
    print(sign)
    # application/json 测试
    params = {
        "windowId": 45,
        "areaId": 10,
        "storeId": "66880000000512",
        "excludeAct": "N",
        "windowType": "ACTIVITY",
        "pageSize": 12,
        "spuSns": [],
        "isFirstRefresh": "TRUE",
        "userKey": "请填写自己的userKey"
    }
    header = {
        "content-type": "application/json",
        "userkey": "请填写自己的userKey",
        "api-version": "V3",
        "api-sign": "d4903f86cb0b258d9723f99a68e1d123",
        "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.15(0x17000f27) NetType/WIFI Language/zh_CN",
        "referer": "https://servicewechat.com/wx6025c5470c3cb50c/170/page-frame.html",
        "api-timestamp": "1598366845610",
    }
    sign = getSign(params, header)
    print(sign)

代码仅供学习交流,请勿用于非法用途

直接上代码

import hashlib
from parser import parseParam
from sign import sign

# 接口秘钥,代码仅供学习,不提供暴露签名
secret = ""

def md5(s):
    m = hashlib.md5()
    m.update(s.encode("utf-8"))
    return str(m.hexdigest()).lower()


def getSign(params, header):
    paramStr = ""
    if header['content-type'] == "application/x-www-form-urlencoded":
        paramStr = parseParam(params)
    else:
        paramStr = str(params)
    return sign(secret, paramStr, header['api-timestamp'])

if __name__ == '__main__':
    # application/x-www-form-urlencoded 测试
    params = {
        "marketingDataQueryReq": '{"spuSns":["20200823000422101803314"],"areaId":10}',
        "userKey": "请填写自己的userKey",
    }
    header = {
        "content-type": "application/x-www-form-urlencoded",
        "userkey": "请填写自己的userKey",
        "api-version": "V3",
        "api-sign": "46a2a47696572c2c087e5534a311237a",
        "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.15(0x17000f27) NetType/WIFI Language/zh_CN",
        "referer": "https://servicewechat.com/wx6025c5470c3cb50c/170/page-frame.html",
        "api-timestamp": "1598366897123",
    }
    sign = getSign(params, header)
    print(sign)
    # application/json 测试
    params = {
        "windowId": 45,
        "areaId": 10,
        "storeId": "66880000000512",
        "excludeAct": "N",
        "windowType": "ACTIVITY",
        "pageSize": 12,
        "spuSns": [],
        "isFirstRefresh": "TRUE",
        "userKey": "请填写自己的userKey"
    }
    header = {
        "content-type": "application/json",
        "userkey": "请填写自己的userKey",
        "api-version": "V3",
        "api-sign": "d4903f86cb0b258d9723f99a68e1d123",
        "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.15(0x17000f27) NetType/WIFI Language/zh_CN",
        "referer": "https://servicewechat.com/wx6025c5470c3cb50c/170/page-frame.html",
        "api-timestamp": "1598366845610",
    }
    sign = getSign(params, header)
    print(sign)

代码仅用于参考学习使用,请勿使用作非法用途

本文地址:https://blog.csdn.net/weixin_43881394/article/details/108258235