简体   繁体   English

Jsoup select-为什么它包含当前元素?

[英]Jsoup select - why does it include current element?

I am trying to understand if I'm missing something, because it seems very bizarre to me why Jsoup includes the current element in the search performed by select . 我试图了解是否丢失了某些内容,因为对于我来说,为什么Jsoup在select执行的搜索中包含当前元素似乎很奇怪。

For example (scala code): 例如(标量代码):

val el = doc.select("div").first
el.select("div").contains(el) // => true

What is the point of this? 这有什么意义呢? I see very limited cases where you'd actually want this. 在少数情况下,您实际上需要这样做。 Do I need to always use el.children.select instead? 我是否需要始终使用el.children.select代替? Is there a nicer method? 有更好的方法吗?

Side question: Is there a nicer way to do el.children.select(s).first ? el.children.select(s).first问题:是否有更好的方法来执行el.children.select(s).first In Ruby Nokogiri it would be el.at_css(s) which is much shorter, is there a similar option in Jsoup? 在Ruby Nokogiri中,它将是el.at_css(s) ,它要短得多,Jsoup中是否有类似的选择?

As to why the select method was implemented the way it did, my only guess would be because it's the most straightforward way to do it if we take into consideration the struct that holds the data resulted by your query. 至于为什么以这种方式实现select方法,我唯一的猜测是,如果考虑到保存查询结果的结构,这是最简单的方法。

If we think about el , we will see that it is a "tree" representation of the elements that you asked for, having as root the first parent div node. 如果考虑el ,我们将看到它是您要的元素的“树”表示形式,以第一个父div节点为根。 Then you call select on that tree. 然后在那棵树上调用select Now it all depends on how you decide to see this tree. 现在,这一切都取决于您决定看这棵树的方式。 Should we treat this "tree" as a whole (include root) or not (discard root)? 我们是否应该整体上(包括根)(丢弃根)对待这棵“树”? It's a matter of taste I guess. 我想这是一个品味问题。

If I judge from myself, a lot of people using Jsoup, probably have had some experience on DOM parsing with jQuery. 如果从我自己判断,很多使用Jsoup的人可能在jQuery的DOM解析方面有一些经验。 The equivalent would be something like this $("div").first().find("div") where find is documented as 等价的东西类似于这样的$("div").first().find("div") ,其中find被记录为

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. 获取当前匹配元素集中每个元素的后代 ,并通过选择器,jQuery对象或元素进行过滤。

This is in agreement with what you stated. 这与您所说的一致。 It's just a matter of how the two libraries "see" the resulting tree. 这只是两个库如何“看到”结果树的问题。 Jsoup treats the root as one of the nodes, jQuery differentiates the root (as far find is concerned). Jsoup将根视为节点之一,jQuery区分根(就find而言)。

About the second part of your question. 关于问题的第二部分。

val el = doc.select("div").first
el.children.select(s).first

No there isn't. 不,没有。 The only way is to change the css selector. 唯一的方法是更改​​css选择器。

val result = doc.select("div:eq(0) " + s).first;

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

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