Server-side jquery

天涯浪子 提交于 2019-11-29 11:21:14

if i understand your question correctly you want is something like phantom.js. PhantomJS is a headless WebKit with JavaScript API. you can inject jquery into it and use all the jquery selectors to manipulate the dom. you can make it work like a standalone server aswell.

Florian Ledermann

I am confused as you have tagged the question with "node.js" though you refer to Python or Perl in your question. Running node.js and/or phantom.js just to run a selector on an HTML DOM sounds quite heavyweight to me, and as always introducing whole chains of dependencies should be considered carefully in real-world projects.

So for Python I would suggest running a combination of BeautifulSoup and soupselect, as mentioned in this answer. You can then do things like:

from BeautifulSoup import BeautifulSoup as Soup
from soupselect import select
import urllib

soup = Soup(urllib.urlopen('http://slashdot.org/'))
select(soup, 'div.title h3')

Note that soupselect seems to implement only a subset of jquery's CSS3 selectors, so for things like e.g. sibling selectors or pseudo-classes it may not work. In this case I would advise to consider porting the relevant part of the project to node.js were you can run Sizzle (jQuery's selector engine) or cheerio standalone in a somewhat lightweight environment.

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