How do I call a Javascript function from Python?

前端 未结 6 790
夕颜
夕颜 2020-11-27 15:12

I am working on a web-scraping project. One of the websites I am working with has the data coming from Javascript.

There was a suggestion on one of my earlier questi

6条回答
  •  隐瞒了意图╮
    2020-11-27 15:59

    An interesting alternative I discovered recently is the Python bond module, which can be used to communicate with a NodeJs process (v8 engine).

    Usage would be very similar to the pyv8 bindings, but you can directly use any NodeJs library without modification, which is a major selling point for me.

    Your python code would look like this:

    val = js.call('add2', var1, var2)
    

    or even:

    add2 = js.callable('add2')
    val = add2(var1, var2)
    

    Calling functions though is definitely slower than pyv8, so it greatly depends on your needs. If you need to use an npm package that does a lot of heavy-lifting, bond is great. You can even have more nodejs processes running in parallel.

    But if you just need to call a bunch of JS functions (for instance, to have the same validation functions between the browser/backend), pyv8 will definitely be a lot faster.

提交回复
热议问题