Evaluate javascript on a local html file (without browser)

﹥>﹥吖頭↗ 提交于 2019-11-27 07:26:20

问题


This is part of a project I am working on for work.

I want to automate a Sharepoint site, specifically to pull data out of a database that I and my coworkers only have front-end access to.

I FINALLY managed to get mechanize (in python) to accomplish this using Python-NTLM, and by patching part of it's source code to fix a reoccurring error.

Now, I am at what I would hope is my final roadblock: Part of the form I need to submit seems to be output of a JavaScript function :| and lo and behold... Mechanize does not support javascript. I don't want to emulate the javascript functionality myself in python because I would ideally like a reusable solution...

So, does anyone know how I could evaluate the javascript on the local html I download from sharepoint? I just want to run the javascript somehow (to complete the loading of the page), but without a browser.

I have already looked into selenium, but it's pretty slow for the amount of work I need to get done... I am currently looking into PyV8 to try and evaluate the javascript myself... but surely there must be an app or library (or anything) that can do this??


回答1:


Well, in the end I came down to the following possible solutions:

  • Run Chrome headless and collect the html output (thanks to koenp for the link!)
  • Run PhantomJS, a headless browser with a javascript api
  • Run HTMLUnit; same thing but for Java
  • Use Ghost.py, a python-based headless browser (that I haven't seen suggested anyyyywhere for some reason!)
  • Write a DOM-based javascript interpreter based on Pyv8 (Google v8 javascript engine) and add this to my current "half-solution" with mechanize.

For now, I have decided to use either use Ghost.py or my own modification of the PySide/PyQT Webkit (how ghost works) to evaluate the javascript, as apparently they can run quite fast if you optimize them to not download images and disable the GUI.

Hopefully others will find this list useful!




回答2:


Well you will need something that both understands the DOM and understand Javascript, so that comes down to a headless browser of some sort. Maybe you can take a look at the selenium webdriver, but I guess you already did that. I don't hink there is an easy way of doing this without running the stuff in an actually browser engine.



来源:https://stackoverflow.com/questions/16375251/evaluate-javascript-on-a-local-html-file-without-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!