Java 将刮取的数据组织到数组中,并从数组中选择1或2个字符串
我想从finviz.com上的一个表中为不同的股票刮取某些值。到目前为止,我只能得到整行并打印出来。我想把返回的值组织成一个数组,这样我就可以单独访问它们Java 将刮取的数据组织到数组中,并从数组中选择1或2个字符串,java,jsoup,Java,Jsoup,我想从finviz.com上的一个表中为不同的股票刮取某些值。到目前为止,我只能得到整行并打印出来。我想把返回的值组织成一个数组,这样我就可以单独访问它们 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
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();
}
//获取数据
试一试{
final Document Document=Jsoup.connect(url.get();
ArrayList dataArray=新的ArrayList();
对于(元素行:document.select(“table.snapshot-table2 tr”)){
//此处不需要继续,只需反转条件即可
如果(!row.select(“td.snapshot-td2:nth类型(10)”).text().contentEquals(“”){
String data=row.select(“td.snapshot-td2:nth of type(10)”).text();
添加(数据);
}
}
System.out.println(dataArray.get(1));
}捕获(例外情况除外){
例如printStackTrace();
}
感谢您抽出时间来帮忙,我非常感谢。这正是我想要的!