丁香园>社区论坛>热门>心情驿站
1、接口确定
2、分析参数情况
3、通过关键字搜索确定加密位置
4、确定加密函数
4.1.1 确定参数noncestr加密函数
4.1.2 参数noncestr解密
4.2.1确定参数sign加密函数
4.2.2参数sign解密,通过代码扣取的方式复现生成sign值,把参数对齐后与元网站的核对一致,说明咱们代码没问题,接下来就是代码整合
5、整合代码后,模拟发送请求,爬取数据
总结:通过这个案例,让我学会了关键字搜索不一定非要搜sign值,通过搜索不容易出现的其他的参数值,更容易定位,然后就是加密参数一定要对齐,否则生成值就会出问题。再着,爬虫要细心细心再细心,更要有耐心。
现成代码如下:
function wordsToBytes(e) {
for (var t = [], n = 0; n < 32 * e.length; n += 8)
t.push(e[n >>> 5] >>> 24 - n % 32 & 255);
return t
}
var i = {
utf8: {
stringToBytes: function (e) {
return i.bin.stringToBytes(unescape(encodeURIComponent(e)))
},
bytesToString: function (e) {
return decodeURIComponent(escape(i.bin.bytesToString(e)))
}
},
bin: {
stringToBytes: function (e) {
for (var t = [], n = 0; n < e.length; n++)
t.push(255 & e.charCodeAt(n));
return t
},
bytesToString: function (e) {
for (var t = [], n = 0; n < e.length; n++)
t.push(String.fromCharCode(e[n]));
return t.join("")
}
}
}
function bytesToWords(e) {
for (var t = [], n = 0, r = 0; n < e.length; n++,
r += 8)
t[r >>> 5] |= e[n] << 24 - r % 32;
return t
}
function stringToBytes(e) {
return i.bin.stringToBytes(unescape(encodeURIComponent(e)))
}
var s = function (e) {
e.constructor == String ? e = stringToBytes(e) : "undefined" !== typeof r && "function" == typeof r.isBuffer && r.isBuffer(e) ? e = Array.prototype.slice.call(e, 0) : Array.isArray(e) || (e = e.toString());
var o = bytesToWords(e)
, i = 8 * e.length
, a = []
, s = 1732584193
, u = -271733879
, c = -1732584194
, l = 271733878
, f = -1009589776;
o[i >> 5] |= 128 << 24 - i % 32,
o[15 + (i + 64 >>> 9 << 4)] = i;
for (var d = 0; d < o.length; d += 16) {
for (var p = s, h = u, v = c, y = l, g = f, m = 0; m < 80; m++) {
if (m < 16)
a[m] = o[d + m];
else {
var b = a[m - 3] ^ a[m - 8] ^ a[m - 14] ^ a[m - 16];
a[m] = b << 1 | b >>> 31
}
var _ = (s << 5 | s >>> 27) + f + (a[m] >>> 0) + (m < 20 ? 1518500249 + (u & c | ~u & l) : m < 40 ? 1859775393 + (u ^ c ^ l) : m < 60 ? (u & c | u & l | c & l) - 1894007588 : (u ^ c ^ l) - 899497514);
f = l,
l = c,
c = u << 30 | u >>> 2,
u = s,
s = _
}
s += p,
u += h,
c += v,
l += y,
f += g
}
return [s, u, c, l, f]
}
function bytesToHex(e) {
for (var t = [], n = 0; n < e.length; n++)
t.push((e[n] >>> 4).toString(16)),
t.push((15 & e[n]).toString(16));
return t.join("")
}
// var e= 'appSignKey=4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3&boardId=15&noncestr=99663351&orderType=3&pageNum=2&pageSize=30&postType=6&serverTimestamp=1719992290151&showType=0×tamp=1719992306412'
function main123(par_f) {
var r = wordsToBytes(s(par_f));
return bytesToHex(r)
}
function u() {
arguments=[8, "number"]
for (var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 8, t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "alphabet", n = "", r = {
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
number: "0123456789"
}[t], o = 0; o < e; o++)
n += r.charAt(Math.floor(Math.random() * r.length));
return n
}
// console.log(u())