jsoup抓取元素处理特定元素
问题描述:
通过了解前一个元素,可以使用jSoup获得继续元素吗?jsoup抓取元素处理特定元素
例如,在这个网站,我有表的“鉴于这一项目”数据
我想获得一个包含邻桌“寻找这个”
<table><tr><td>irrelevant info 1 <a href="http://jsoup.org/">jsoup</a></td></tr></table>
<p>there is a p here</p>
<table><tr><td>Given this item <a href="http://jsoup.org/">jsoup</a></td></tr></table>
<p>there is a p here</p>
<table><tr><td>Looking for this <a href="http://jsoup.org/">jsoup</a></td></tr></table>
<p>there is a p here</p>
<table><tr><td>irrelevant info 2<a href="http://jsoup.org/">jsoup</a></td></tr></table>
<p>there is a p here</p>
<table><tr><td>irrelevant info 3 <a href="http://jsoup.org/">jsoup</a></td></tr></table>
答
或者您可以使用list.indexOf
Elements tables = doc.select("table");// returns a list of all table elements
Element given = doc.select("table:contains(Given this item)").first(); //yor given element
Element required = tables.get(tables.indexOf(given)+1);//index of given + 1 = index of required element
答
谢谢TDG
siblingA〜siblingX:找到同胞A之前的兄弟X元素,例如, H1〜p
所以我落得这样做:
table:contains(Given this item) ~ table
然后我把e.first()
这很奇怪 - 我我试过使用'〜'但出于某种原因,我没有得到预期的结果。 – TDG
请将此添加到您的解决方案 - 我会接受它。 – user648026
但它不适用于我,所以我使用了我的答案中描述的方法。你不需要接受我的答案。如果你的解决方案有效,你应该接受它,所以其他用户会知道它解决了你的问题。 – TDG