【发布时间】:2016-09-14 22:56:05
【问题描述】:
我正在尝试从 HTML 表格中提取一些信息并将它们放入例如 arraylist = new ArrayList<HashMap<String, String>>(); 以便在我的应用程序中更好地管理。
在发布请求后,我已经能够将正确的 HTML 页面保存在我的 document 变量中。
以下是包含我有用数据的 HTML 片段,但它不是页面内唯一的表格。我不知道如何在这个特定的表中找到项目。
以这种格式获取数据的正确方法是:DAY - TIME - SUGGESTION?
非常感谢您提前提供任何建议!
<table><tbody>
<tr><th class="date">Wed, 14 Sep 2016</th><th></th><th></th></tr>
<tr><td> </td><td class="sub">09:00</td><td class="sugg">Depart and set your watch to the arrival city's time zone (03:00). Sleep as needed. The following times are in the arrival city's time zone.</td></tr>
<tr><td> </td><td class="sub">18:30</td><td class="sugg">Arrive</td></tr>
<tr><td> </td><td class="sub">19:00–22:00</td><td class="sugg">Seek light</td></tr>
<tr><td> </td><td class="sub">22:00–23:00</td><td class="sugg">Avoid light before bed</td></tr>
<tr><td> </td><td class="sub">23:00–07:00</td><td class="sugg">Sleep ideal</td></tr>
<tr><th class="date">Thu, 15 Sep 2016</th><th></th><th></th></tr>
<tr><td> </td><td class="sub">20:00–23:00</td><td class="sugg">Seek light before bed</td></tr>
<tr><td> </td><td class="sub">23:00–07:00</td><td class="sugg">Sleep ideal</td></tr>
<tr><th class="date">Fri, 16 Sep 2016</th><th></th><th></th></tr>
<tr><td> </td><td class="sub">20:00–23:00</td><td class="sugg">Seek light before bed</td></tr>
<tr><td> </td><td class="sub">23:00–07:00</td><td class="sugg">Sleep ideal</td></tr>
</tbody></table>
编辑
我认为循环是我想要实现的方式。我越来越接近解决方案。我需要找到一种方法来检测我在循环中检查的当前行是否有 th 或 td 单元格:
//find the table, it is the second table in the HTML
Element table = document.select("tbody").get(1);
//get all the rows
Elements rows = table.select("tr");
//loop the rows
for (Element row : rows) {
//if the row contains th, I get the first cell and save day in a string
//if the row contains td, I get the second (time) and third (suggestion) cells and put in my map string with day, time, suggestion
}
【问题讨论】:
标签: android web-scraping jsoup