У меня есть следующий XML
<div id = "Master25" class = "open-pane" role = "tabpanel">
<a href = "/option1" title = "option1" class = "top-element ">
<div class = "top-element-description">
<div class = "top-element-option-title">Spain</div>
<div class = "top-element-option-description">Test1</div>
</div>
<span class = "top-element-price">23€</span>
</a>
<a href = "/option2" class = "top-element ">
<div class = "top-element-description">
<div class = "top-element-option-title">Greece</div>
<div class = "top-element-option-description">Test2</div>
</div>
<span class = "top-element-price">25€</span>
</a>
<a href = "/option3" class = "top-element ">
<div class = "top-element-description">
<div class = "top-element-option-title">Germany</div>
<div class = "top-element-option-description">Test3</div>
</div>
<span class = "top-element-price">26€</span>
</a>
<a href = "/option4" class = "top-element ">
<div class = "top-element-description">
<div class = "top-element-option-title">Austria</div>
<div class = "top-element-option-description">Test4</div>
</div>
<span class = "top-element-price">29€</span>
</a>
Как сделать селектор так, чтобы я получал:




Как насчет дочернего селектора?
a > div
Если я тестирую в Jsoup онлайн
С вашим вкладом я получаю:
Spain Test1
Greece Test2
Germany Test3
Austria Test4
Вы можете использовать jsop, как показано ниже:
String html = new String(Files.readAllBytes(Paths.get("test.txt")));
Document doc = Jsoup.parse(html);
Elements elements = doc.body().getElementsByClass("top-element-description");
for (Element el : elements) {
System.out.println(el.text());
}
Выход:
Spain Test1
Greece Test2
Germany Test3
Austria Test4
Спасибо за быстрый ответ. Похоже, это решило мою проблему.