Converting an HTML table from an external URL into a local CSV file

chrysaetos99 :

I'd like to save an HTML table (for example the first table in this Wikipedia article) as a CSV-file in Java. It should not only work with that one table, but with all kinds of tables (the user should be able to enter a link to a table and the program just automatically converts it to CSV).

I already tried to work with jsoup, but I don't get how it works at the moment.

How can I convert an HTML table stored at some external URL to CSV and save it as a file?

Steffi :
public static void main(String[] args) throws Exception {

    String url = "https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_the_United_States";
    try{
        Document doc = Jsoup.connect(url).get();
        Element table = doc.getElementById("covid19-container");
        Elements rows = table.getElementsByTag("tr");

        for(Element row : rows){
            System.out.println(row.text());
        }

    }catch (IOException e){
        e.printStackTrace();
    }
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409395&siteId=1