JSoup extract table as CSV from finance website

£可爱£侵袭症+ 提交于 2019-12-08 17:48:25
Alkis Kalogeris

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