这是 HTML 结构:
Element link = doc.select("div.subtabs p").first();
这似乎不起作用。如何选择那个 p
?
最佳答案
class="subtabs"的DIV 实际上不是p
元素的父元素,而是p
的兄弟元素。要检索 p
,您需要首先获取对具有 id="content":
Element link = doc.select("div#content > p").first();
此外,您需要 >
符号来表明您正在选择 div#content 的子项。
parent > child: child elements that descend directly from parent, e.g. div.content > p finds p elements; and body > * finds the direct children of the body tag
如果您以后遇到 JSOUP CSS 选择器的问题,请查看 JSOUP Selector Syntax cookbook ,其中有一些很好的例子和解释。
关于java - 如何在 JSOUP 中选择此元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10695835/