首页 > 解决方案 > 使用 Jsoup 库从 android 中的网站获取 html 表的数据,

问题描述

我正在开发一个应用程序,我正在解析一个或两个网站的一些数据。幸运的是,我为我的一些目标数据做到了,但没有。现在我正在使用 Jsoup 解析来自网站的数据,我使用相同的 jsoup 格式来获取第 2 阶段的数据,就像我为我的应用程序的第 1 阶段所做的那样,但这次没有获取显示为空白的 arraylist。我检查了两个 HTML 代码,两者都有一些不同。

在我的第一阶段,我使用它的类解析了表格,然后我得到了该表格的相应内容。在第二阶段,表格的格式及其 tr & tds 是不同的,所以我很难弄清楚。我正在发布要从中获取数据的 html 代码。

<div class="view-content">
  <table class="views-table cols-3">
    <thead>
    </thead>
    <tbody>
      <tr class="odd views-row-first views-row-last">
        <td class="views-field views-field-counter">
          1 </td>
        <td class="views-field views-field-body">
          <p>some text here</p>
        </td>
        <td class="views-field views-field-field-notif-pdf">
          <a href="https://someurl.pdf" target="_blank"></a> Size :- 1.85 MB, Language:- English</td>
      </tr>
    </tbody>
  </table>
</div>

我想要上面表格标记中的数据,我无法弄清楚如何处理 tr 和 td 中的所有类。任何帮助或建议将不胜感激..

谢谢你!

标签: javaandroidhtmljsouphtml-parsing

解决方案


您可以在 Jsoup 中使用选择器:

 File input = new File("path_to_html/test.html");
        Document doc = Jsoup.parse(input, StandardCharsets.UTF_8.name());
///select table body
        Element tbody = doc.select("tbody").first();

其他示例:

https://jsoup.org/cookbook/extracting-data/selector-syntax


推荐阅读