Android的Jsoup帮助 - 如何从表中获取元素中的文本?

时间:2017-11-03 03:34:32

标签: java android jsoup

http://msu.esuds.net/RoomStatus/showRoomStatus.i?locationId=1016364

当我检查链接中的表格时,表格的名称为table.room_status

但是当我试着告诉洗衣机/干衣机是否可用时:

private void getWebsite(){
    Log.d("getWebsite","runs");
    new Thread(new Runnable() {
        @Override
        public void run() {
            Log.d("run","runs");
            final TextView textView = (TextView)findViewById(R.id.parseTest);
            final StringBuilder builder = new StringBuilder();
            try {
                Document doc = Jsoup.connect("http://msu.esuds.net/RoomStatus/showRoomStatus.i?locationId=1028671").get();
                for (Element table : doc.select("table.room_status")) {
                    Log.d("table","runs");
                    for (Element row : table.select("tr")) {
                        Log.d("row","runs");
                        Elements tds = row.select("td");
                        if (tds.size() == 6) {
                            Elements font = tds.get(4).getElementsByTag("font");
                            builder.append(font.first().text());
                            Log.d("font","runs");
                        }
                    }
                }
            } catch (IOException e){
                builder.append("Error: ").append(e.getMessage()).append("\n");
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(builder.toString());
                }
            });
        }
    }).start();

}

我看到的唯一记录是Log.d("getWebsite", "runs)Log.d("run", "runs")

1 个答案:

答案 0 :(得分:2)

打开浏览器的开发者工具,您在加载网站时会看到GET请求后,您有POST请求获取该表。更改您的请求以获取该页面。将用户代理字符串添加到请求中也很有用:

Document doc = Jsoup.connect("http://msu.esuds.net/RoomStatus/machineStatus.i?bottomLocationId=1016486")
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .post();