1

I am starting to study some web technologies to integrate content, markup, layout, styling and behaviors of stuff for personal use (NOT web developing for now) and am amazed with the power of JQuery selectors and functions.

I have heard that there are some ways to use javascript "outside" a browser, to do some DOM selection, manipulation, etc. I wonder if JQuery could be used that way too.

So, what I would like to do is:

  • Using some programming/scripting language (I use Python), access a XML file and parse its DOM;
  • Programmatically manipulate and modify the DOM with javascript/jquery selectors and functions;
  • Save the results to (possibly another) XML file.
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • I use [HTA](http://msdn.microsoft.com/en-us/library/ms536496%28v=vs.85%29.aspx) for that since it gives you a filesystem object too. HTA is however just IE in a box – mplungjan Mar 07 '12 at 14:18
  • 1
    jQuery relies on the DOM functions which only exist in web-browsers... – Šime Vidas Mar 07 '12 at 14:19
  • @hippietrail jQuery uses the browser's DOM methods when performing DOM querying, DOM traversal, and DOM manipulation. Just look at jQuery's source code. I don't know what you mean by your comment... – Šime Vidas Aug 17 '12 at 12:44
  • @ŠimeVidas: Sorry my "not any more" referred not to jQuery relying on the DOM functions. It referred to the DOM functions only existing in web browsers. I've been reading of more and more tools which include their own DOM implementation. One seems to be [`jsdom`](https://github.com/tmpvar/jsdom). I think `node.js` provides a DOM too. – hippietrail Aug 17 '12 at 12:55
  • Just to be sure, I asked a question here that people can point to or find via Google: **[Are there implementations of the W3C DOM other than in web browsers?](http://stackoverflow.com/questions/12006490/are-there-implementations-of-the-w3c-dom-other-than-in-web-browsers)** – hippietrail Aug 17 '12 at 13:28
  • 1
    @hippietrail Ah, I see. Yes, my original comment is not correct. DOM implementations are *not* limited to browsers. – Šime Vidas Aug 17 '12 at 15:57

4 Answers4

3

If you like jQuery syntax, check out pyQuery:

from pyquery import PyQuery

_ = PyQuery('<body><p></p></body>')
_("p").text("hello").css({'color': 'red'})
print _.html()


>>> <p style="color: red">hello</p>
georg
  • 211,518
  • 52
  • 313
  • 390
1

yeah, you just need a Javascript run time.

Check out node.js

Mild Fuzz
  • 29,463
  • 31
  • 100
  • 148
1

What you're looking for is called a "headless" browser.

This SO post may help:

Real headless browser

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

Basically you need a javascript interpreter (ex: V8) + wrapper for your language of choice (ex: pyv8). Then you can do this (from pyv8 page):

import PyV8
ctxt = PyV8.JSContext()
ctxt.enter()
ctxt.eval("1+2") # 1+2 is a javascript code
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54