简体   繁体   English

服务器端jquery

[英]Server-side jquery

Say I have a script written in perl or python. 假设我有一个用perl或python编写的脚本。 What's the easiest way to write a function that would use jquery selectors on strings as part of it? 编写一个在字符串上使用jquery选择器作为其中一部分的函数最简单的方法是什么? ie to be able to do: 即能够做到:

jquery_selector('table.new#element', text)

where jquery_selector is a function that runs a jquery selector on the html string stored in text . 其中jquery_selector是一个在text存储的html字符串上运行jquery选择器的函数。 Even if it was just limited to returning strings (not full jquery objects), it would still be really useful. 即使它仅限于返回字符串(不是完整的jquery对象),它仍然非常有用。 ie if you were required to give a javascript function as a callback which would render the results to something comprehensible in the scripting language: 即如果你被要求提供一个javascript函数作为回调,它会使结果呈现在脚本语言中易于理解的东西:

jquery_selector('table.new#element, text, 'function(e){return e.val()}')

And it would return the results of the callback as a list. 它会将回调的结果作为列表返回。

I realize that there are dom libraries for most languages, but jquery is so much better than most of them. 我意识到大多数语言都有dom库,但jquery比大多数语言都要好得多。

I am not asking about native libraries which have a syntax like jquery. 我不是要问有像jquery这样的语法的本地库。 I guess what would be needed is an API to a browser which jquery would run on? 我想需要的是一个jquery运行的浏览器的API? Or is this what node.js does? 或者这是node.js的作用?

if i understand your question correctly you want is something like phantom.js . 如果我理解你的问题你想要的是像phantom.js PhantomJS is a headless WebKit with JavaScript API. PhantomJS是一个带有JavaScript API的无头WebKit。 you can inject jquery into it and use all the jquery selectors to manipulate the dom. 你可以将jquery注入其中并使用所有jquery选择器来操作dom。 you can make it work like a standalone server aswell. 你可以使它像独立的服务器一样工作。

I am confused as you have tagged the question with "node.js" though you refer to Python or Perl in your question. 我很困惑,因为你已经用“node.js”标记了这个问题,尽管你在问题中引用了Python或Perl。 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. 运行node.js和/或phantom.js只是为了在HTML DOM上运行一个选择器对我来说听起来非常重要,并且在实际项目中应该仔细考虑引入整个依赖链。

So for Python I would suggest running a combination of BeautifulSoup and soupselect , as mentioned in this answer . 因此对于Python,我建议运行BeautifulSoupsoupselect的组合,如本答案中所述 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 eg sibling selectors or pseudo-classes it may not work. 请注意,soupselect似乎只实现了jquery的CSS3选择器的一个子集,因此对于诸如兄弟选择器或伪类之类的东西,它可能不起作用。 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. 在这种情况下,我建议考虑将项目的相关部分移植到node.js,你可以在一个有点轻量级的环境中运行Sizzle (jQuery的选择器引擎)或cheerio standalone。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM