gpt4 book ai didi

java - 使用 Jsoup 从网页中读取指定行的文本

转载 作者:行者123 更新时间:2023-12-01 09:39:49 29 4
gpt4 key购买 nike

所以我试图从 this webpage 获取数据使用 Jsoup...

我尝试过查找多种不同的方法,并且已经接近目标,但我不知道如何查找某些统计数据的标签(攻击力量防御等)

举个例子,我想打印出来

'Attack', '15', '99', '200,000,000' 

我应该怎样做呢?

最佳答案

您可以使用CSS selectorsJsoup轻松提取列数据。

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com