【问题标题】:Extract correctly info from table with JSoup in Android在 Android 中使用 JSoup 从表中正确提取信息
【发布时间】: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>&nbsp;</td><td class="sub">09:00</td><td class="sugg">Depart and set your watch to the arrival city&#39;s time zone (03:00). Sleep as needed. The following times are in the arrival city&#39;s time zone.</td></tr>
<tr><td>&nbsp;</td><td class="sub">18:30</td><td class="sugg">Arrive</td></tr>
<tr><td>&nbsp;</td><td class="sub">19:00&ndash;22:00</td><td class="sugg">Seek light</td></tr>
<tr><td>&nbsp;</td><td class="sub">22:00&ndash;23:00</td><td class="sugg">Avoid light before bed</td></tr>
<tr><td>&nbsp;</td><td class="sub">23:00&ndash;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>&nbsp;</td><td class="sub">20:00&ndash;23:00</td><td class="sugg">Seek light before bed</td></tr>
<tr><td>&nbsp;</td><td class="sub">23:00&ndash;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>&nbsp;</td><td class="sub">20:00&ndash;23:00</td><td class="sugg">Seek light before bed</td></tr>
<tr><td>&nbsp;</td><td class="sub">23:00&ndash;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


    【解决方案1】:

    所以你在这里有两个选择,你可以利用css选择器按类拉出所有元素。

    https://try.jsoup.org/

    或者你可以遍历元素。

    Document doc = Jsoup.connect(url).get();
    Element div = doc.select("tbody").first();
     for (Element element : div.children()) {
        //do stuff here
    }
    

    【讨论】:

    • 我决定追求循环解决方案,但我不明白如何识别我是否与 th 或 td 元素连续
    • 我找到了解决方案。无论如何,谢谢你的回答;)
    【解决方案2】:

    好吧,我想出了一个解决方案,也许不是最好的编码风格,但它有效:)(工程师:“如果有效,那就好”)

    我对某些语言的编码有一定的了解,但这是我第一次不得不处理解析和 JSoup。它不是一个那么容易理解的工具,但在我的研究中我注意到它非常强大。我把它放在我的个人学习列表中。

    注意:这种方法假设在 td 行之前总是有第 th 行。

    这是我的解决方案:

            String day = null;
            String time;
            String sugg;
    
            //crop the page in order to leave the table I needed, since it was without a specific id, I selected it as the second table in the page
            Element table = document.select("tbody").get(1);
    
            //this is the list of all the row in the table
            Elements rows = table.select("tr");
    
            //here I cycle the rows
            for (Element row : rows) {
    
                HashMap<String, String> map = new HashMap<String, String>();
    
    
                //if the row contains th elements, I store the first th of the row as day
                if (!row.select("th").isEmpty())
                {
                    day = row.select("th").get(0).text();
                }
    
                //if the row contains td elements, I store the second and third td in strings and put all in map
                if (!row.select("td").isEmpty())
                {
                    time = row.select("td").get(1).text();
                    sugg = row.select("td").get(2).text();
    
                    Log.d("row: ", day + " " + time + " " + sugg);
    
                    map.put("day", day);
                    map.put("time", time);
                    map.put("sugg", sugg);
                }
    
                arraylist.add(map);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多