views:

165

answers:

4

I need to fetch some result on a webpage, which use some javascript code to generate the part I am interesting in like following

eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--)d[c]=k[c]||c;k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('5 11=17;5 12=["/3/2/1/0/13.4","/3/2/1/0/15.4","/3/2/1/0/14.4","/3/2/1/0/7.4","/3/2/1/0/6.4","/3/2/1/0/8.4","/3/2/1/0/10.4","/3/2/1/0/9.4","/3/2/1/0/23.4","/3/2/1/0/22.4","/3/2/1/0/24.4","/3/2/1/0/26.4","/3/2/1/0/25.4","/3/2/1/0/18.4","/3/2/1/0/16.4","/3/2/1/0/19.4","/3/2/1/0/21.4"];5 20=0;',10,27,'40769|54|Images|Files|png|var|imanhua_005_140430179|imanhua_004_140430179|imanhua_006_140430226|imanhua_008_140430242|imanhua_007_140430226|len|pic|imanhua_001_140429664|imanhua_003_140430117|imanhua_002_140430070|imanhua_015_140430414||imanhua_014_140430382|imanhua_016_140430414|sid|imanhua_017_140430429|imanhua_010_140430289|imanhua_009_140430242|imanhua_011_140430367|imanhua_013_140430382|imanhua_012_140430367'.split('|'),0,{}))

The result of eval() is valuable to me, I am writing a Python script, is there any library I can use to virtually run this piece of javascript code and get the output?

Thanks

+2  A: 

pyv8 is a set of bindings for the V8 JavaScript Engine (Google Chrome)

henrikh
love to see so many choices, between spidermonkey and v8, it's a matter of preferring firefox or chrome, Thanks!
overboming
+1  A: 

This seems to be suitable for my need: http://code.google.com/p/python-spidermonkey/

overboming
I've also found a very simple command line tool built in on Mac OS called jrunscript, this works out of the box.
overboming
A: 

You can use PyQt with the WebKit module :) It has JS engine and can evaluate JS within context of a (X)HTML document.

Viet
Which btw uses SquirrelFish (Extreme) from Safari/Webkit on supported platforms.
henrikh
Oh, and you can also use QScript which is more efficient because it does not have the DOM or rendering to care about. (Uses SquirrelFish too)
henrikh
+3  A: 

Use a spidermonkey binding

from spidermonkey import Runtime
rt = Runtime()
cx = rt.new_context()
result = cx.eval_script(whatyoupostedabove)
duncan
This project seems to be dead for a long time, and won't work on snow leopard
overboming
I use the ruby version on Linux, and it is also kind of dead. But it works. The biggest problem on ruby is not the bindings themselves but to get the right spidermonkey (did not work with 1.9 for example).
duncan