JSoup extract table as CSV from finance website

佐手、 提交于 2019-12-08 07:51:27

问题


My problem is the following: I want to extract a table from an html file downloaded from a website using JSoup and return it as csv-file. (The data is historic stock prices).

Here is the website: http://www.finanzen.ch/kurse/historisch/Actelion/VIRTX/12.6.2013_17.9.2013

It is in German, so I hope this is no problem. I want to extract the table with all the numbers.

I have got the following code so far:

    Document doc = Jsoup.connect("http://www.finanzen.ch/kurse/historisch/Actelion/VIRTX/12.6.2013_17.9.2013").get();

    for (Element table : doc.select("table.Historische Kurse Actelion Ltd.*")) {
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
            if (tds.size() > 6) {
                System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
            }
        }
    }

I got this code from another StackOverflow article. The problem is I don't know anything about JSoup and I'm quite new to programming in Java. I would greatly appreciate your help.


回答1:


Try this

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {

    public static void main(String[] args) {

        String url = "http://www.finanzen.ch/kurse/historisch/Actelion/VIRTX/12.6.2013_17.9.2013";
        Document doc;
        try {
            doc = Jsoup.connect(url).get();
            Element table = doc
                    .select("div.mainwrapper div.main_background div.main_left")
                    .get(0).child(3);
            Elements rows = table.select("tr");

            Elements ths = rows.select("th");

            String thstr = "";
            for (Element th : ths) {
                thstr += th.text() + " ";
            }
            System.out.println(thstr);

            for (Element row : rows) {
                Elements tds = row.select("td");
                for (Element td : tds) {
                    System.out.println(td.text());  // --> This will print them
                                                    // individually
                }
                System.out.println(tds.text()); // --> This will print everything
                                                // in the row
            }
            // System.out.println(table);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


来源:https://stackoverflow.com/questions/18847960/jsoup-extract-table-as-csv-from-finance-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!