我试图从这个网站的表中获得前五名的最高百分比收益并将它们存储到一个数组中。我想打印前五名最高百分比收益。 http://www.wsj.com/mdc/public/page/2_3021-gainnyse-gainer.html
截至目前,我的代码获取所有行和列并在输出中打印它们。我只能获得前五名,并将它们存储到我的阵列中。
请帮忙。
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();
}
}
Curent输出是:
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......
答案 0 :(得分:0)
我使用地图作为存储数据,股票名称的名称(我认为)和现在的价值如果数据总是这样它会起作用但我建议问网站管理员可能有一个简单的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));
}
}