gpt4 book ai didi

java - 使用 JSoup 提取表数据

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:04 25 4
gpt4 key购买 nike

我正在尝试使用 JSoup 从表中提取财务信息。我已经审查了类似的问题,并且可以让他们的例子发挥作用(这里有两个:

Using Jsoup to extract data

Using JSoup To Extract HTML Table Contents )。

我不确定为什么代码不能在 my URL 上运行。

以下是 3 种不同的尝试。任何帮助将不胜感激。

String s = "http://financials.morningstar.com/valuation/price-ratio.html?t=AXP&region=usa&culture=en-US";

//Attempt 1
try {
Document doc = Jsoup.connect("http://financials.morningstar.com/valuation/price-ratio.html?t=AXP&region=USA&culture=en_US").get();

for (Element table : doc.select("table#currentValuationTable.r_table1.text2")) {
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
if (tds.size() > 6) {
System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
}
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
// Attempt 2
try {
Document doc = Jsoup.connect(s).get();
for (Element table : doc.select("table#currentValuationTable.r_table1.text2")) {
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
for (int i = 0; i < tds.size(); i++) {
System.out.println(tds.get(i).text());
}
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
//Attempt 3
try {
Document doc = Jsoup.connect(s).get();
Elements tableElements = doc.select("table#currentValuationTable.r_table1.text2");

Elements tableRowElements = tableElements.select(":not(thead) tr");

for (int i = 0; i < tableRowElements.size(); i++) {
Element row = tableRowElements.get(i);
System.out.println("row");
Elements rowItems = row.select("td");
for (int j = 0; j < rowItems.size(); j++) {
System.out.println(rowItems.get(j).text());
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}

最佳答案

Psherno 提供的答案:

Print what Document was able to read from page (use System.out.println(doc);). Something tells me that your problem may be related with fact that HTML content you are looking for is dynamically added by JavaScript by browser, which Jsoup can't do since it doesn't have JavaScript support. In that case you should use more powerful tool like web driver (like Selenium).

关于java - 使用 JSoup 提取表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30831173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com