所以我试图从 this webpage 获取数据使用 Jsoup...
我尝试过查找多种不同的方法,并且已经接近目标,但我不知道如何查找某些统计数据的标签(攻击、力量、防御等)
举个例子,我想打印出来
'Attack', '15', '99', '200,000,000'
我应该怎样做呢?
最佳答案
您可以使用CSS selectors在 Jsoup轻松提取列数据。
// retrieve page source code
Document doc = Jsoup
.connect("http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Lynx%A0Titan")
.get();
// find all of the table rows
Elements rows = doc.select("div#contentHiscores table tr");
ListIterator<Element> itr = rows.listIterator();
// loop over each row
while (itr.hasNext()) {
Element row = itr.next();
// does the second col contain the word attack?
if (row.select("td:nth-child(2) a:contains(attack)").first() != null) {
// if so, assign each sibling col to variable
String rank = row.select("td:nth-child(3)").text();
String level = row.select("td:nth-child(4)").text();
String xp = row.select("td:nth-child(5)").text();
System.out.printf("rank=%s level=%s xp=%s", rank, level, xp);
// stop looping rows, found attack
break;
}
}
关于java - 使用 Jsoup 从网页中读取指定行的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38548297/