js2py的使用

 基本使用:

import execjs
import  js2py
un ='''
function sample(x)
{
    return func2(x)
}
'''
print(js2py.eval_js(un)("Hi"))
import js2py

js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
""".replace("document.write", "return ")
result = js2py.eval_js(js)


# 第二种:执行js库或代码多,函数多的时候
with open('./get_key.js') as fp:
    js = fp.read()
    # ctx2 = execjs.compile(js)
    context = js2py.EvalJs()
    context.execute(js)
    vl5x=context.getKey(cookie)

js内部数据向Python代码中传递

gen_guid="""
function createGuid() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
var guid1 = createGuid() + createGuid() + "-" + createGuid() + "-" + createGuid() + createGuid() + "-" + createGuid() + createGuid() + createGuid();

"""
    context = js2py.EvalJs()
    context.execute(gen_guid)
    guid=context.guid1 # 将guid1传递到Python中
    print guid

编码问题

在js2py执行base64.js的时候,遇到0xe4
‘utf8’ codec can’t decode byte 0xe4 in position 28: invalid continuation byte

使用decode(‘latin1’)解码js

get_docid = js2py.eval_js(DocID_js.decode('latin1'))

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值