2017-09-25 3 views
0

이전 요소를 알면 jSoup를 사용하여 진행 요소를 가져올 수 있습니까?jsoup 요소 가져 오기 특정 요소 진행

이 HTML에서, 예를 들어, 나는 "이 항목을 감안할 때 "테이블의 데이터를
나는

<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> 

예를 들어 "이을 찾고 "를 포함하는 다음 테이블을 가져 싶습니다 : http://try.jsoup.org/~vtmUE0bVgNHSxdvpKcIzpL3pHEA

답변

1

당신이 이상한

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 
1

은에 따르면

당신에게 TDG 감사 jsoup cookbook

siblingA ~ siblingX : 형제 A가 앞에 오는 형제 X 요소를 찾습니다. H1 ~ P

그래서 나는 일을 결국 :

table:contains(Given this item) ~ table 

을 그리고 나는() 또는

+1

list.indexOf 사용할 수 있습니다 e.first했다 - I '~'을 사용하려했지만 어떤 이유로 나는 예상 된 결과를 얻지 못했습니다. – TDG

+0

해결책에이를 추가하십시오. 동의하겠습니다. – user648026

+1

그러나 그것은 나를 위해 작동하지 않았다, 그래서 나는 내 대답에 설명 된 방법을 사용했습니다. 당신이 나에게 대답을 받아 들일 필요는 없습니다. 솔루션이 작동하는 경우이를 받아 들여야하므로 다른 사용자가 문제를 해결했음을 알 수 있습니다. – TDG