Python爬虫,请求参数加密怎么办?

563 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情

 背景介绍:

大家好 我是政胤.

我们在请求接口的时候,发现请求参数数加密的,该如何处理呢?今天介绍两种方式完成请求参数解密,一种是通过调试js,逆向解析的方式,另一种是Python方式实现解析请求参数。

目标网址:

https://www.oklink.com/zh-cn/btc/tx-list?limit=20&pageNum=1

页面分析:

确定数据接口地址

编辑

确定请求头和请求参数有没有加密

编辑

编辑

确定返回的数据是不是加密

编辑

逆向解析加密参数思路

定位到加密参数所在哪个js文件

编辑

编辑

在来源面板中打开js

编辑

确定加密参数所在位置

编辑

打断点,刷新页面

编辑

确定getApiKey方法的位置

编辑

改写js

编辑

编辑

补全js代码,并改成成函数

编辑

运行js

编辑

编辑

编辑

编辑

代码实现:

code_js.js

function getApiKey() {
    var t = (new Date).getTime()
      , e = encryptApiKey();
    return t = encryptTime(t),
    comb(e, t)
}
// encryptApiKeya
function encryptApiKey() {
    var t = "a2c903cc-b31e-4547-9299-b6d07b7631ab"
      , e = t.split("")
      , r = e.splice(0, 8);
    return e.concat(r).join("")
}
// encryptApiKey
function encryptTime(t) {
    var e = (1 * t + 1111111111111).toString().split("")
      , r = parseInt(10 * Math.random(), 10)
      , n = parseInt(10 * Math.random(), 10)
      , o = parseInt(10 * Math.random(), 10);
    return e.concat([r, n, o]).join("")
}
// comb
function comb(t, e) {
    var r = "".concat(t, "|").concat(e);
    return btoa(r)
}

// 调用函数运行
// console.log(getApiKey())

python代码实现

import base64 import random

import requests import json from jsonpath import jsonpath import time

def getApiKey(): # 13位的时间戳 t = int(str(time.time() * 1000)[:-5]) e = encryptApiKey() # print('t的值', e) t = encryptTime(t) # print('e的值', t) return comb(e, t)

def encryptApiKey(): t = "a2c903cc-b31e-4547-9299-b6d07b7631ab" # e是t后28个字符组成的列表 e = [j for j in t[8:]] # r是前8个字符组成的列表 r = [j for j in t[:8]] # 返回值就是 e拼接r组成的字符串 e.extend(r) return ''.join(e)

def encryptTime(t): # e 为 (1 * t + 1111111111111)的结果转为字符串的每个字符组成的列表 e = [j for j in str(1 * t + 1111111111111)] # r、n、o 为 随机数字(0-9) r = str(random.randint(0, 9)) n = str(random.randint(0, 9)) o = str(random.randint(0, 9)) # 返回的结果是 e,r, n, o 拼接后的字符串 e.extend(list(r + o + n)) return ''.join(e)

def comb(t, e): # r 是t 和"|" 和e 拼接之后的字符串 r = t + '|' + e # 返回的是base64编码的字符串 return base64.b64encode(r.encode()).decode()

def parse(offse): apikey = getApiKey() print(apikey) # print(apiKey) header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36", 'x-apiKey': apikey } # t 是当前时间的数据戳 t = str(time.time() * 1000)[:-5] # print(t) data = { 't': t, 'limit': 20, 'offset': offse } # 数据接口地址 url = 'www.oklink.com/api/explore…' res = requests.get(url, headers=header, params=data).text # json字符串数据,转为python字典数据 dict_data = json.loads(res) # print(json_data) # 数据提取 # 交易哈希 hash_list = jsonpath(dict_data, "..hash") # 所在区块 blockHeight_list = jsonpath(dict_data, "..blockHeight") # 数量(BTC) inputsValue_list = jsonpath(dict_data, "..inputsValue") # 手续费(BTC) fee_list = jsonpath(dict_data, "..fee") print(hash_list, blockHeight_list, inputsValue_list, fee_list)

爬取3页

for i in range(1, 3): print(f'正在爬取第{i}页') offset = (i - 1) * 20 parse(offset)

总结:

````js`

通过这个案例,我们可以知道怎样确定数据接口地址,怎样确定加密参数所在哪个js文件以及了解js调试过程。通过调试js逆向解析的方式和Python方式均可实现请求参数解析。python实现的原理就是将js的写函数用python的方式来写。

##                                    我是政胤 期待你的关注