【问题标题】:Storing my output scraped from website into an array and printing specific part of it将从网站上抓取的输出存储到一个数组中并打印它的特定部分
【发布时间】:2018-08-08 19:02:54
【问题描述】:

我试图从这个网站的表格中获得前五名的最高百分比收益并将它们存储到一个数组中。我想打印前 5 名的最高百分比收益。 http://www.wsj.com/mdc/public/page/2_3021-gainnyse-gainer.html

截至目前,我的代码获取所有行和列并将它们打印在输出中。我无法仅获得前 5 个并将它们存储到我的数组中。

请帮忙。

public static void main(String[] args) throws IOException {
    Document doc = Jsoup.connect("http://www.wsj.com/mdc/public/page/2_3021-gainnyse-gainer.html").get();
    Elements rows = doc.select("tr");
    for(Element row :rows)
    {
        Elements columns = row.select("td");
        String[][] trtd = new String[columns.size()][];
        for (Element column:columns)
        {
            System.out.println(column.text());
        }
        System.out.println();
    }

}

目前的输出是:

SEARCH
Issue(Roll over for charts and headlines)
Price
Chg
% Chg
Volume

1
PHH (PHH)
$10.71
2.19
25.65
10,865,948

2
Chico's Fas (CHS)
10.03
1.35
15.63
4,514,899

3
Veeva Systems Cl A (VEEV)
70.48
8.41
13.55
3,300,989

4
Tutor Perini (TPC)
24.70
2.85
13.04
1,723,950

5
TriNet Group (TNET)
46.93
5.35
12.87
1,089,758

6
Nelnet Cl A (NNI)
57.60
5.99
11.61
121,379

7
Federal Signal (FSS)
21.35
1.74
8.87
272,982

etc......

【问题讨论】:

  • 您是否在每种情况下都按此顺序获取它们?
  • 我没有得到与你相同的输出。

标签: java arrays sorting html-table jsoup


【解决方案1】:

我使用地图作为存储数据,股票名称的名称(我认为)和当前的值,如果数据总是这样,它会起作用,但我建议询问网站管理员也许有一个简单的 api

  public static void main(String[] args) throws IOException {
    Document doc = Jsoup.connect("http://www.wsj.com/mdc/public/page/2_3021-gainnyse-gainer.html").get();
    Elements rows = doc.select("tr");

    Map<Integer, HashMap<String, String>> top5 = new HashMap<>(5);

    int arrayFill = 0;
    for (int i = 0; i < rows.size(); i++) {
        Elements columns = rows.get(i).select("td");
        String[][] trtd = new String[columns.size()][];
        for (Element column : columns) {
            System.out.println(column.text());
        }
        System.out.println();
        if (i > 2 &&i <8&& columns.size() > 4) {
            HashMap<String, String> map = new HashMap<>(1);
            map.put(columns.get(1).text(), columns.get(4).text());
            top5.put(Integer.parseInt(columns.get(0).text()), map);
        }

    }
    System.out.println("using keySet");
    for (Integer key : top5.keySet()) {
        System.out.println(key + "=" + top5.get(key));
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多