本文介绍了JSoup提取表作为csv从金融网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:我想从使用JSoup从网站下载的html文件中提取表,并将其返回为csv-file。 (数据是历史股票价格)。

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).

这是网站:

这是德语,所以我希望这没有问题。我想提取所有数字的表。

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

我有以下代码:

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

StackOverflow文章。问题是我不知道任何关于JSoup和我是新的编程Java。非常感谢您的帮助。

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.

感谢

推荐答案

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
                                                    // indiviadually
                }
                System.out.println(tds.text()); // -->This will pring everything
                                                // in the row
            }
            // System.out.println(table);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

这篇关于JSoup提取表作为csv从金融网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 06:18
查看更多