将抓取的数据组织成一个数组,并从数组中选择 1 或 2 个字符串
Organizing scraped data into an array and selecting 1 or 2 Strings from the array
我想从 finviz.com 的 table 中抓取不同股票的某些值。到目前为止,我只能得到一整行并将其打印出来。我想将返回值组织到一个数组中,以便我可以单独访问它们。
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class WebScrape {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("Ticker: ");
String userInput = scanner.next();
final String url = "https://finviz.com/quote.ashx?t=" + userInput;
// Get data
try {
final Document document = Jsoup.connect(url).get();
for (Element row : document.select("table.snapshot-table2 tr")) {
if (row.select("td.snapshot-td2:nth-of-type(10)").text().contentEquals("")) {
continue;
} else {
final String data = row.select("td.snapshot-td2:nth-of-type(10)").text();
System.out.println(data);
{
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
现在,当您 运行 这样做时,您会得到一整行。我不知道如何将返回值放入数组以及如何将第二个值访问到数组中。
只需使用 ArrayList 并在每次迭代中添加您的结果。然后像在任何数组中一样获得第二个结果,在索引 1:
// Get data
try {
final Document document = Jsoup.connect(url).get();
ArrayList<String> dataArray = new ArrayList<>();
for (Element row : document.select("table.snapshot-table2 tr")) {
// continue was unnecessary here, just invert the condition
if (!row.select("td.snapshot-td2:nth-of-type(10)").text().contentEquals("")) {
String data = row.select("td.snapshot-td2:nth-of-type(10)").text();
dataArray.add(data);
}
}
System.out.println(dataArray.get(1));
} catch (Exception ex) {
ex.printStackTrace();
}
我想从 finviz.com 的 table 中抓取不同股票的某些值。到目前为止,我只能得到一整行并将其打印出来。我想将返回值组织到一个数组中,以便我可以单独访问它们。
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class WebScrape {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("Ticker: ");
String userInput = scanner.next();
final String url = "https://finviz.com/quote.ashx?t=" + userInput;
// Get data
try {
final Document document = Jsoup.connect(url).get();
for (Element row : document.select("table.snapshot-table2 tr")) {
if (row.select("td.snapshot-td2:nth-of-type(10)").text().contentEquals("")) {
continue;
} else {
final String data = row.select("td.snapshot-td2:nth-of-type(10)").text();
System.out.println(data);
{
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
现在,当您 运行 这样做时,您会得到一整行。我不知道如何将返回值放入数组以及如何将第二个值访问到数组中。
只需使用 ArrayList 并在每次迭代中添加您的结果。然后像在任何数组中一样获得第二个结果,在索引 1:
// Get data
try {
final Document document = Jsoup.connect(url).get();
ArrayList<String> dataArray = new ArrayList<>();
for (Element row : document.select("table.snapshot-table2 tr")) {
// continue was unnecessary here, just invert the condition
if (!row.select("td.snapshot-td2:nth-of-type(10)").text().contentEquals("")) {
String data = row.select("td.snapshot-td2:nth-of-type(10)").text();
dataArray.add(data);
}
}
System.out.println(dataArray.get(1));
} catch (Exception ex) {
ex.printStackTrace();
}