@bergus
2015-12-12T10:49:34.000000Z
字数 1030
阅读 1974
python
pyv8
折腾一下午终于弄好PyV8引擎,解析JS方便多了.
关键: PyV8依赖于Boost,所以安装前先确定你的Linux有没有Boost(就因为这个折腾一下午,官方首页也没说,找了好久)
Ubuntu下安装
sudo apt-get install libboost-all-dev
sudo apt-get install aptitude
接着安装PyV8
用pip安装
$sudo pip install -v pyv8
或:
sudo apt-get install scons libboost-python-dev
svn checkout http://v8.googlecode.com/svn/trunk/ v8
svn checkout http://pyv8.googlecode.com/svn/trunk/ pyv8
cd v8
export V8_HOME=`/home/buffer/v8`
cd ../pyv8
sudo python setup.py build
sudo python setup.py install
接着就可以快乐的使用PyV8了....附使用代码
import PyV8
class v8Doc(PyV8.JSClass):
def write(self, s):
print s.decode('utf-8')
class Global(PyV8.JSClass):
def __init__(self):
self.document = v8Doc()
glob = Global()
ctxt = PyV8.JSContext(glob)
ctxt.enter()
ctxt.eval("var ant=8257+4341;var calf=5749+403^ant;var goat=1986+4175^calf;var fish=6422+7944^goat;var worm=7920+3648^fish;")
ctxt.eval("document.write(((10502^calf)+24).toString());")
jss = "document.write((10502^calf)+24);"
jss = "var thisvalue = " + jss.strip().replace("document.write(","").replace(");",";")
print jss
ctxt.eval(jss)
vars = ctxt.locals.thisvalue
print "vars = " + str(vars)