PyExceJS 爬虫执行js代码

爬虫中的js混淆

# js 混淆 :
	1. 开发人员将javascript代码,利用js加密工具进行加密,生成看不懂的js代码

# 暴力破解 js混淆:
	1. 将源代码 放到破解网站可以的到能看懂的js代码
	2. 破解网站: http://www.bm8.com.cn/jsConfusion

爬虫的加密和解密

# 场景: 开发人员会将加密和解密的信息放在js代码中.
# python 利用PyExceJS模块可以让python执行js代码
	pip install PyExceJS

PyExceJS 练习

# -*-coding:utf-8-*-
#  pip3 install PyexceJS

import execjs
#### 需要 安装 nodejs的开发环境
#### 获取一个 node 对象 ,  能够执行 js代码生成环境的对象
import requests

node = execjs.get()

# params js函数中的参数
method = 'GETCCITYWEATHER'
city = '北京'
type = 'HOUR'
start_time = '2018-01-25 00:00:00'
end_time = '2018-01-25 23:00:00'

# Complie javascript 准 破解后的js代码
# 本地的js代码
file = 'test.js'
# 生成 js的环境
ctx = node.compile(open(file, encoding='utf-8').read())

# 1. 加密 
encryption_js = 'getPostParamCode("{0}","{1}","{2}","{3}","{4}")'.format(method, city, type, start_time, end_time)
# 执行 js 代码
params = ctx.eval(encryption_js)
# print(params)

# 获取数据
URL = 'https://www.aqistudy.cn/apinew/aqistudyapi.php'

response_text = requests.get(url=URL,params=params).text

# 2 . 解密
decrypt_js = 'decodeData("{}")'.format(response_text)
ctx2 = node.compile(open(file, encoding='utf-8').read())
decrypt_data = ctx2.eval(decrypt_js)

print(decrypt_data)

原文地址:https://www.cnblogs.com/dengz/p/14802131.html