好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

爬虫工程师也应该会的 NodeJS 知识(三)- 快速抛弃 execjs

什么是 Express ?

Express 是一个基于 NodeJS 的 Web Server 开发框架,能够帮助我们快速的搭建 Web 服务器

为什么需要 Express ?

1、不使用框架,使用原生的 NodeJS 开发 Web 服务器,我们需要处理很多繁琐且没有技术含量的内容,例如:获取路由,处理路由等等

2、 不使用框架,使用原生的 NodeJS 开发 Web 服务器,需要解析 get、post 参数解析,使用 Express 可以使用现成的插件实现上面的功能,只要关心核心的业务逻辑即可

3、Python 中的 execjs 库已经停止更新,存在很多未知 bug,使用 express 不管从性能上还是易用性上都要高出一筹

如何使用 Express ?

手动安装

 npm?install?express  

简单使用

 const?express?=?require("express") const?app?=?express() app.get('/',function(req,res){ ????res.send('hello,express') }) app.listen(3000,()=>{ ????console.log("监听端口3000成功") })  

返回静态资源

 const?express?=?require("express") const?path?=?require("path") const?app?=?express() app.get('/',function(req,res){ ????res.send('hello,express') }) //?这里的?pathname?是存放静态资源的路径 app.use(express.static(path.join(__dirname,'pathname'))); app.listen(3000,()=>{ ????console.log("监听端口3000成功") })  

获取 ?get ?请求参数

 const?express?=?require("express") const?app?=?express() app.get('/',function(req,res){ ????res.send(req.query) }) app.listen(3000,()=>{ ????console.log("监听端口3000成功") })  

获取 ?get ?请求参数测试结果

获取 post 请求参数

安装
 npm?install?body-parser  
例子
 const?express?=?require("express"); const?bodyParser?=?require('body-parser');  const?app?=?express(); //?app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended?:?false}));  app.post('/',function(req,res){ ????res.send(req.body['name']) });  app.listen(3000,()=>{ ????console.log("监听端口3000成功") });  
获取 post 请求参数截图

Express 在 ?Js ?逆向中的应用

通过上面的两个例子已经可以学会关于 express 是如何处理请求参数的了,现在就把它应用到 Js 逆向中

在之前我们处理 Js 加密使用的是? python 的? execjs

这个包已经很久没有更新了,经常会出现一些未知的 bug ,所以我们今天就要放弃 execjs 使用 express 来处理加密的? js

直接上一段之前文章的测试代码

Python 爬虫进阶必备 | 某视频数据分析平台加密参数分析

Python + execjs 版本:

 import?requests import?execjs #?用?postman?直接生成的,勿喷 url?=?"https://xd.newrank.cn/xdnphb/nr/cloud/douyin/rank/hotAccountAllRankList"  #?这里的?crack_xd.js?就是?js?加密逻辑 with?open('crack_xd.js',"r")?as?f: ????js_data?=?f.read() ????js_data?=?execjs测试数据pile(js_data)  params?=?js_data.call("get_params","/xdnphb/nr/cloud/douyin/rank/hotAccountAllRankList") print(params) payload?=?"{\"date\":\"2020-08-16\",\"date_type\":\"days\",\"type\":\"娱乐\",\"start\":1,\"size\":20}" headers?=?{ ????'authority':?"xd.newrank.cn", ????'pragma':?"no-cache", ????'cache-control':?"no-cache,no-cache", ????'accept':?"application/json,?text/plain,?*/*", ????'user-agent':?"Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/83.0.4103.116?Safari/537.36", ????'content-type':?"application/json;charset=UTF-8", ????'origin':?"https://xd.newrank.cn", ????'sec-fetch-site':?"same-origin", ????'sec-fetch-mode':?"cors", ????'sec-fetch-dest':?"empty", ????'referer':?"https://xd.newrank.cn/data/tiktok/rank/overall", ????'accept-language':?"zh-CN,zh;q=0.9,en;q=0.8", ????'cookie':?"Hm_lvt_e20c9ff085f402c8cfc53a441378ca86=1597660797;?Hm_lpvt_e20c9ff085f402c8cfc53a441378ca86=1597660859;?token=62621CBEE73B4CF98CAA79A77958EA9D", ????'Postman-Token':?"30dbcaa8-0e0e-44f0-b3ad-b6ddf9c90921" ????}  response?=?requests.request("POST",?url,?data=payload.encode(),?headers=headers,?params=params) print(response.text)  

Python + express 版本:

 import?requests import?execjs  url?=?"https://xd.newrank.cn/xdnphb/nr/cloud/douyin/rank/hotAccountAllRankList"  def?get_params(): ????params_url?=?"http://localhost:3000" ????headers?=?{ ????????'Connection':?"keep-alive", ????????'Accept-Language':?"zh-CN,zh;q=0.9,en;q=0.8", ????????'Content-Type':?"application/x-www-form-urlencoded", ????????'cache-control':?"no-cache", ????}  ????payload?=?"callback=u_params('/xdnphb/nr/cloud/douyin/rank/hotAccountAllRankList')" ????response?=?requests.post(params_url,?headers=headers,?data=payload)  ????return?response.text   params?=?get_params()  payload?=?"{\"date\":\"2020-08-16\",\"date_type\":\"days\",\"type\":\"娱乐\",\"start\":1,\"size\":20}" headers?=?{ ????'authority':?"xd.newrank.cn", ????'pragma':?"no-cache", ????'cache-control':?"no-cache,no-cache", ????'accept':?"application/json,?text/plain,?*/*", ????'user-agent':?"Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/83.0.4103.116?Safari/537.36", ????'content-type':?"application/json;charset=UTF-8", ????'origin':?"https://xd.newrank.cn", ????'sec-fetch-site':?"same-origin", ????'sec-fetch-mode':?"cors", ????'sec-fetch-dest':?"empty", ????'referer':?"https://xd.newrank.cn/data/tiktok/rank/overall", ????'accept-language':?"zh-CN,zh;q=0.9,en;q=0.8", ????'cookie':?"token=62621CBEE73B4CF98CAA79A77958EA9D;?Hm_lvt_e20c9ff085f402c8cfc53a441378ca86=1597660797,1598777678;?Hm_lpvt_e20c9ff085f402c8cfc53a441378ca86=1598777678;?_uab_collina=159877767862449280501573", ????'Postman-Token':?"30dbcaa8-0e0e-44f0-b3ad-b6ddf9c90921" }  response?=?requests.request("POST",?url,?data=payload.encode(),?headers=headers,?params=params)  print(response.text)  

express 代码样例:

 const?express?=?require("express"); const?bodyParser?=?require('body-parser'); const?app?=?express();  app.use(bodyParser.urlencoded({extended?:?false})); /* 中间省略加密的逻辑代码 */ app.post('/',function(req,res){ ????let?params?=?req.body['callback']; ????console.log(params) ????let?value?=?eval(params); ????res.send(value) });  app.listen(3000,()=>{ ????console.log("监听端口3000成功") });  

代码运行结果

以上就是这次的全部内容了,咱们下次再会 ~

Love&Share?

查看更多关于爬虫工程师也应该会的 NodeJS 知识(三)- 快速抛弃 execjs的详细内容...

  阅读:34次