第十五题说难也难说不难也不难,就是会遇到新的知识,遇到不懂的还不简单,百度呀。
地址:
http://match.yuanrenxue.com/match/15
目录:
1、环境
2、分析请求
3、实现爬取
1、环境
Python3.7、pywasm、requests
2、分析请求

第一步找到数据接口,并查看加密参数是什么,如图2-1
第二步查看调用栈,并进入代码,如图2-2


var list = {
"m": window.m(),
"page": window.page,
};
可以看到m参数为window.m(),window.m()是一个函数
window.m = function (){
t1 = parseInt(Date.parse(new Date())/1000/2);
t2 = parseInt(Date.parse(new Date())/1000/2 - Math.floor(Math.random() * (50) + 1));
return window.q(t1, t2).toString() + '|' + t1 + '|' + t2;
};
t1、t2就是时间戳经过一定的计算得出,t2中有一个随机数,既然是随机数那就直接定为1呗,反正系统怎么知道是随机多少
返回值是window.q(t1, t2)和t1、t2组合而成
fetch('/static/match/match15/main.wasm').then(response =>
response.arrayBuffer()
).then(bytes => WebAssembly.instantiate(bytes)).then(results => {
instance = results.instance;
window.q = instance.exports.encode;
fetch('/static/match……有点类似于python调用js的情况
window.q为wasm文件中的encode方法,既然JavaScript可以调用那用python应该也能调用,百度查到一个pywasm,这个库就可以调用wasm文件,用法基本和pyexejs一样
基本用法:
vm = pywasm.load("./main.wasm")
result = vm.exec("目标方法", [参数1, 参数2])
分析到这里就基本完成了
3、代码实现
import time
import pywasm
import requests
headers = {
"User-Agent": "yuanrenxue.project",
}
def get_m():
timestamp = int(time.time()) * 1000
t1 = int(timestamp / 1000 / 2)
t2 = int(timestamp / 1000 / 2 - 51)
vm = pywasm.load("./main.wasm")
result = vm.exec("encode", [t1, t2])
return f'{result}|{t1}|{t2}'
def main():
sum_list = []
for page in range(1, 6):
url = f'http://match.yuanrenxue.com/api/match/15?m={get_m()}&page={page}'
response = requests.get(url=url, headers=headers)
for i in response.json()['data']:
sum_list.append(i['value'])
print(sum(sum_list))
if __name__ == '__main__':
main()