Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Python work with javascript

I am working on a scrapy app to scrapte some data on a web page

But there is some data loaded by ajax, and thus python just cannot execute that to get the data.

Is there any lib that simulate the behavior of a browser?

like image 330
enzo Avatar asked Aug 31 '26 23:08

enzo


2 Answers

For that you'd have to use a full-blown Javascript engine (like Google V8 in Chrome), to get the real functionality of the browser and how it interacts. However, you could possibly get some information by looking up all URLs in the source and doing a request to each, hoping for some valid data. But in overall, you're stuck without a full Javascript engine.

Something like python-spidermonkey. A wrapper to the Javascript engine of Mozilla. However using it might be rather complicated, but that's dependant on your specific application.

You'd basically have to build a browser, but seems Python-people have made it simple. With PyWebkitGtk you'd get the dom and using either python-spidermonkey mentioned before or PyV8 mentioned by Duncan you'd theoretically get the full functionality needed for a browser/webscraper.

like image 124
zatatatata Avatar answered Sep 03 '26 14:09

zatatatata


The problem is that you don't just have to be able to execute some Javascript (that's easy), you also have to emulate the browser DOM and that's a lot of work.

If you want to be able to run Javascript then you can use PyV8. Install it with easy_install PyV8 and then you can execute any standalone javascript:

>>> import PyV8
>>> ctxt = PyV8.JSContext()
>>> ctxt.enter()
>>> ctxt.eval("(function(a,b) { return [a+b, a*b, a/b, a-b] })(13,29)")
<_PyV8.JSArray object at 0x01F26A30>
>>> list(_)
[42, 377, 0.4482758620689655, -16]

You can also pass in classes defined in Python, so in principle might be able could emulate enough of the DOM for your purposes.

like image 27
Duncan Avatar answered Sep 03 '26 13:09

Duncan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!