js2py的使用


基本使用:

1 import execjs
2 import  js2py
3 un ='''
4 function sample(x)
5 {
6     return func2(x)
7 }
8 '''
9 print(js2py.eval_js(un)("Hi"))

 

 1 import js2py
 2 
 3 js = """
 4 function escramble_758(){
 5 var a,b,c
 6 a='+1 '
 7 b='84-'
 8 a+='425-'
 9 b+='7450'
10 c='9'
11 document.write(a+c+b)
12 }
13 escramble_758()
14 """.replace("document.write", "return ")
15 result = js2py.eval_js(js)
16 
17 
18 # 第二种:执行js库或代码多,函数多的时候
19 with open('./get_key.js') as fp:
20     js = fp.read()
21     # ctx2 = execjs.compile(js)
22     context = js2py.EvalJs()
23     context.execute(js)
24     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'))


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM