简体   繁体   English

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

[英]Jsoup Help for Android - How do i get the text in an element from a table?

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

When I inspect element the table in the link provided the table's name is table.room_status . 当我检查元素时,如果表的名称为table.room_status ,则链接中的表为。

But when I try to tell if the washers/dryers are available with: 但是当我尝试告诉洗衣机/烘干机是否可用于:

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();

}

The only logcats I see are Log.d("getWebsite", "runs) and Log.d("run", "runs") . 我看到的唯一logcat是Log.d("getWebsite", "runs)Log.d("run", "runs")

Open your browser's developer tools and you'll see that when loading the site, after the GET request you have a POST request that fetches the table. 打开浏览器的开发人员工具,您会看到在加载网站时,在GET请求之后,您将获得一个POST请求来提取表。 Change your request to get that page. 更改您的请求以获取该页面。 It is also useful to add the user-agent string to the request: 将用户代理字符串添加到请求中也很有用:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM