提问人:Kit 提问时间:3/7/2023 最后编辑:AbraKit 更新时间:3/7/2023 访问量:48
Jsoup 和 for 循环 nosuchelementexception
Jsoup and for loop nosuchelementexception
问:
我正在尝试通过不断更改 URL 字符串来收集 Yahoo Finance 上的数据。
这是我的代码,更有意义:
public static void gatherLoop() throws NoSuchElementException, IOException {
for (var i = 1; i < 99; i++) {
BufferedReader gatherStocks = new BufferedReader(new FileReader("stockNames.txt"));
Stream<String> load = Files.lines(Paths.get("stockNames.txt"));
String loadString = load.skip(i).findFirst().get();
String glURL = "http://finance.yahoo.com/quote/"+loadString+"?p="+loadString+"&.tsrc=fin-srch";
if (glURL.equals("http://finance.yahoo.com/quote/?p=&.tsrc=fin-srch")) {
gatherStocks.close();
System.out.println("Loaded Stocks");
System.out.println("If Their Is No Stocks Popping Up, You Have No Saved Stocks");
}
else {
Document doc = Jsoup.connect(glURL).get();
Elements contentA = doc.getElementsByClass("D(ib) Fz(18px)");
System.out.println("|------------------------------------------------|");
for(Element a : contentA) {
System.out.println("| " + i + ". Name: " + a.text());
gatherStocks.readLine();
}
}
}
}
但是,当我运行代码时,我在编译器中收到运行时错误:
Exception in thread "main" java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:143)
at StockAnalysis.saves.gatherLoop(saves.java:32)
at StockAnalysis.saves.gatherSavedStocks(saves.java:25)
at StockAnalysis.Home.main(Home.java:27)
我尝试的第一件事就是再次运行,但这会重置为零。然后我试着只读字符串。i++
void
i
loadstring
答:
0赞
Abra
3/7/2023
#1
你似乎使它变得比它需要的要复杂得多。
这是我stocknames.txt文件:
RIVN
SNAP
NVOS
AMAM
TRKA
BBIO
SAVE
UNCY
AAPL
NTNX
SOFI
GLPH
GBTC
每行都是股票代码。
下面的代码读取文件stocknames.txt的每一行,并针对读取的每一行调用输出与给定符号关联的名称的方法。我相信这就是你正在努力做的事情。showName
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Extractor {
private static final String TEMPLATE = "http://finance.yahoo.com/quote/%s?p=%s&.tsrc=fin-srch";
public static void showName(String name) throws IOException {
String url = String.format(TEMPLATE, name, name);
Connection conn = Jsoup.connect(url);
Document doc = conn.get(); // throws java.io.IOException
Elements contentA = doc.getElementsByClass("D(ib) Fz(18px)");
for (Element a : contentA) {
System.out.println(a.text());
}
}
public static void main(String[] args) {
try (Scanner gatherStocks = new Scanner(new File("stocknames.txt"))) {
while (gatherStocks.hasNextLine()) {
String name = gatherStocks.nextLine();
showName(name);
}
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
当我运行上面的代码时,我得到以下输出:
Rivian Automotive, Inc. (RIVN)
Snap Inc. (SNAP)
Novo Integrated Sciences, Inc. (NVOS)
Ambrx Biopharma Inc. (AMAM)
Troika Media Group, Inc. (TRKA)
BridgeBio Pharma, Inc. (BBIO)
Spirit Airlines, Inc. (SAVE)
Unicycive Therapeutics, Inc. (UNCY)
Apple Inc. (AAPL)
Nutanix, Inc. (NTNX)
SoFi Technologies, Inc. (SOFI)
Grayscale Bitcoin Trust (BTC) (GBTC)
评论
0赞
Kit
3/7/2023
谢谢,我稍后会尝试这种方法。
0赞
Kit
3/8/2023
有没有办法没有主方法,因为我的主要方法是主屏幕。
0赞
Abra
3/8/2023
@Kit方法可以放入您喜欢的任何方法中。main
0赞
Kit
3/9/2023
对不起,我是 java 的新手。
评论
stockNames.txt
String loadString = load.skip(i).findFirst().get();
Optional
for
i
findFirst
Optional
Stream