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

标签 java html jsoup screen-scraping

所以我试图从 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/

相关文章:

java - Spring Boot - 如何在 Spring RestController 的 map 中获取所有请求参数?

java - 使用假列表类

html - 当其他人悬停时保持相邻可见

java - 在 Jsoup 中再次将原始 html 字符串拆分为行

java - 获取jsoup中html字符串中的所有属性

java - 批处理文件未在 Windows 8.1 中执行所需的命令

java - Maven 找不到我的自定义 Maven 原型(prototype)

java - 玩框架。无需编译

html - 创建表后巨大的差距

javascript - 引导覆盖不生效