基本使用:
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'))