简体   繁体   English

Jsoup-取得父母

[英]Jsoup - Get Parent

I have some HTML and I am trying to get a link, and I know a few qualities the link I want will have so I can find it easily, but not I also want to get the <h2 calss='locs'> tag above the link I have. 我有一些HTML,并且正在尝试获取链接,并且我知道我想要的链接具有一些品质,因此我可以轻松找到它,但我也不想在上方获得<h2 calss='locs'>标签我有链接。 So the html might look like this 因此,html可能看起来像这样

<html>
    <body>
        <h2 class = 'locs'>text</h2>
        <p>
           <a link and stuff...>
        </p>
        <h2 class = 'locs'>foo</h2>
        <p>
           <a another link and stuff...>
        </p>
    </body>
</html>

But the <h2> isn't always just the first parent of the <a> so is there a way to search for the that comes before the link? 但是<h2>并不总是<a>的第一个父项,因此有没有办法搜索链接之前的?

Thanks for the help 谢谢您的帮助

I am not totally sure what you are asking, but this is what I assume: You want the first a Element after a h2 Element of class locs . 我不确定您要问的是什么,但这是我的假设:您想要在class locsh2 Element之后的第a Element。

To do this, you can#t use the parent() method, since the a elements parents seem to be p elements which are in turn siblings (not children) of the h2.locs Elements. 为此,您不能使用parent()方法,因为a元素的父元素似乎是p元素,而这些元素又是h2.locs元素的h2.locs元素(而不是子元素)。 Here is what i would do: 这是我会做的:

Elements as = doc.select("h2.locs + p>a");

Explanation: 说明:

  • h2.locs looks for h2 elments with class locs h2.locs使用类locs查找h2 h2.locs
  • A + B looks for B which must be direct sibling to the previous A. A + B查找必须与先前A直接同级的B。
  • p>a looks for a elements which are children of p p>a寻找a它们的子元素p

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

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