html Java Jsoup循环打印
我对Java和Jsoup都是新手。我已经成功地使用Jsoup获取了网站数据。然而,目前我正在打印搜索结果标题,它们都在一行返回。我想将结果嵌套在for循环中,这样搜索标题结果就可以打印在每一行上
This is how the website is formatted:
<ul>
<li><span class ="title">Title 1<span></li>
<li><span class ="title">Title 2<span></li>
<li><span class ="title">Title 3<span></li>
</ul>
我的代码返回:标题1标题2标题3
My Java code:
public class WebScrape {
public static void main (String [] args) throws Exception {
final Document document = Jsoup.connect("website.com").get();
String searchResultsTitle = document.select("span[class=title]").text();
System.out.println(searchResultsTitle);
}
}
# 1 楼答案
Document.select
返回org.jsoup.select.Elements
,每个Element
都是一个span
和class=title
,因此您可以在Elements
中存储标题,并对其进行迭代,而不是调用.text()