【问题标题】:How to fetch tabular data from the web to an Android app using Java如何使用 Java 将表格数据从 Web 获取到 Android 应用程序
【发布时间】:2020-03-23 20:00:38
【问题描述】:

我正在尝试使用 Java 从https://www.worldometers.info/coronavirus/ 获取报告冠状病毒病例表数据到我的 Android 应用程序。

所以我有一个名为 country 的对象,该对象的实例是 countryName、TotalCase、Total death 等,然后我将把该对象存储在一个 arrayList 中。

这是我的代码:

import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class SammaryHandler extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        try{
            Document document= Jsoup.connect("https://www.worldometers.info/coronavirus/").userAgent("mozilla/17.0").get();
//            Elements temp= document.select("table table-bordered table-hover main_table_countries dataTable no-footer");

            Elements temp= document.select("table.dataTable");//?????????????????

            int size= temp.size();
            for(int i=0; i<size;i++){
                String data= temp.select("table.dataTable").eq(i).attr("");//?????????????
                System.out.println(data);
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
}

那么我怎样才能从表中读取所有数据并将负载保存在对象中?

【问题讨论】:

    标签: java android web-scraping


    【解决方案1】:

    是的,只是作为他们网站上的信息 - 未经许可不得使用他们的数据。

    但是回答你的问题。我主要看到您没有应用正确的 CSSQuery。 试试这个代码:

          //connect to the website
    final String url =
                    "https://www.worldometers.info/coronavirus/";
    //surround by try catch
            try {
                final Document document = Jsoup.connect(url).get();
        //create a list of what you need from the table. we will read data by each column
                List<String> countrieNames = null;
    //loop by elements in the table
            for (Element row : document.select("table#main_table_countries_today tr")) {
    //filter as there are empty rows or others to be filtered
                            if (row.select("td:nth-of-type(1)").text().equals("") || row.select("td:nth-of-type(1)").text().equals("Total:")) {
                                  continue;
                                }
    //save informations you need to the created list
                                countrieNames = document.select("td:nth-of-type(1)").eachText();
    
            }
    //print out results
    for (int i = 0; i < countrieNames.size(); i=i+2) {
                        System.out.println(countrieNames.get(i));
                    }
    } catch(Exception e){
                    e.printStackTrace();
                }
    

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多