本文主要是介绍Python 3 执行 JavaScript 脚本,js2py教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装库
pip3 install js2py
1 2 | pip3 install js2py |
使用教程
➜ ~ ipython3 Python 3.6.5 |Anaconda custom (x86_64)| (default, Apr 26 2018, 08:42:37) Type 'copyright', 'credits' or 'license' for more information IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import js2py In [2]: js2py.eval_js('var a = "hello"; a') Out[2]: 'hello'
1 2 3 4 5 6 7 8 9 10 | ➜ ~ ipython3 Python 3.6.5 | Anaconda custom ( x86_64 ) | ( default , Apr 26 2018 , 08 : 42 : 37 ) Type 'copyright' , 'credits' or 'license' for more information IPython 6.1.0 -- An enhanced Interactive Python . Type '?' for help . In [ 1 ] : import js2py In [ 2 ] : js2py . eval_js ( 'var a = "hello"; a' ) Out [ 2 ] : 'hello' |
执行js 函数
>>> import js2py >>> add = js2py.eval_js('function add(a, b) {return a + b}') >>> add(1, 2) + 3 6
1 2 3 4 5 | >>> import js2py >>> add = js2py . eval_js ( 'function add(a, b) {return a + b}' ) >>> add ( 1 , 2 ) + 3 6 |
In [3]: import js2py ...: js = js2py.EvalJs({}) ...: js.execute(""" ...: var i = 0; ...: for(;;i++) { ...: break; ...: } ...: // i must be 0. ...: console.log("i should be 0. i=" + i); ...: """) ...: 'i should be 0. i=1'
1 2 3 4 5 6 7 8 9 10 11 12 13 | In [ 3 ] : import js2py . . . : js = js2py . EvalJs ( { } ) . . . : js . execute ( """ ...: var i = 0; ...: for(;;i++) { ...: break; ...: } ...: // i must be 0. ...: console.log("i should be 0. i=" + i); ...: """ ) . . . : 'i should be 0. i=1' |
总结:
现在写爬虫很多的网站都开始用js 加密技术了,所以这个库必须要学的,不然你就没法玩了
这篇关于Python 3 执行 JavaScript 脚本,js2py教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!