js2py的使⽤
基本使⽤:
1 import execjs2 import js2py3 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执⾏base.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'))