安装库
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 加密技术了,所以这个库必须要学的,不然你就没法玩了