[英]Storing my output scraped from website into an array and printing specific part of it
我正在嘗試從該網站的表格中獲得最高的前五個百分比收益,並將它們存儲到一個數組中。 我想打印前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......
我使用地圖作為存儲數據,股票名稱的名稱(按照我的想法)和當前值(如果數據始終像這樣)可以使用,但我建議您詢問網站管理員,也許有一個簡單的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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.