无法抓取标题
Can't scrape title
我已经成功地从一个页面收集了我想要的所有数据,但我不明白为什么我无法从同一年龄提取标题或股票代码。 None 我尝试过的方法有效。
感谢任何能提供帮助的人。
我编写的初始代码运行不佳,此站点的人已经帮助解决了它。我知道 table 名称是正确的,但我似乎无法弄清楚为什么它不起作用。仅供参考,我想要获得的是图表下方的股票代码和公司名称。
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;
try {
final Document document = Jsoup.connect(url).get();
ArrayList<String> dataArray = new ArrayList<>();
for (Element row : document.select("table.fullview-title tr")) {
if ( !row.select("td.fullview-title:nth-of-
type(2)").text().contentEquals("")) {
String data = row.select("td.fullview-title:nth-of-
type(2)").text();
dataArray.add(data);
}
System.out.println(dataArray);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我没有收到任何错误,我可以轻松连接到 url,但代码只是 returns 一个空值。
我认为您需要更改选择器。
"table.fullview-title tr" -> "table.fullview-title tr td"
"td.fullview-title:nth-of-type(2)" -> "a.fullview-ticker"
希望对您有所帮助:
public class DemoApplication {
public static void main(String[] args) {
// Simplification:
// Scanner scanner = new Scanner(System.in);
// System.out.println("Ticker: ");
// String userInput = scanner.next();
// final String url = "https://finviz.com/quote.ashx?t=" + userInput;
final String url = "https://finviz.com/quote.ashx?t=LCI";
try {
final Document document = Jsoup.connect(url).get();
ArrayList<String> dataArray = new ArrayList<>();
for (Element row : document.select("table.fullview-title tr td")) {
if (!row.select("a.fullview-ticker").text().contentEquals("")) {
String data = row.select("a.fullview-ticker").text();
dataArray.add(data);
}
}
System.out.println(dataArray);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
输出:
[LCI]
我已经成功地从一个页面收集了我想要的所有数据,但我不明白为什么我无法从同一年龄提取标题或股票代码。 None 我尝试过的方法有效。
感谢任何能提供帮助的人。
我编写的初始代码运行不佳,此站点的人已经帮助解决了它。我知道 table 名称是正确的,但我似乎无法弄清楚为什么它不起作用。仅供参考,我想要获得的是图表下方的股票代码和公司名称。
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;
try {
final Document document = Jsoup.connect(url).get();
ArrayList<String> dataArray = new ArrayList<>();
for (Element row : document.select("table.fullview-title tr")) {
if ( !row.select("td.fullview-title:nth-of-
type(2)").text().contentEquals("")) {
String data = row.select("td.fullview-title:nth-of-
type(2)").text();
dataArray.add(data);
}
System.out.println(dataArray);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我没有收到任何错误,我可以轻松连接到 url,但代码只是 returns 一个空值。
我认为您需要更改选择器。
"table.fullview-title tr" -> "table.fullview-title tr td"
"td.fullview-title:nth-of-type(2)" -> "a.fullview-ticker"
希望对您有所帮助:
public class DemoApplication {
public static void main(String[] args) {
// Simplification:
// Scanner scanner = new Scanner(System.in);
// System.out.println("Ticker: ");
// String userInput = scanner.next();
// final String url = "https://finviz.com/quote.ashx?t=" + userInput;
final String url = "https://finviz.com/quote.ashx?t=LCI";
try {
final Document document = Jsoup.connect(url).get();
ArrayList<String> dataArray = new ArrayList<>();
for (Element row : document.select("table.fullview-title tr td")) {
if (!row.select("a.fullview-ticker").text().contentEquals("")) {
String data = row.select("a.fullview-ticker").text();
dataArray.add(data);
}
}
System.out.println(dataArray);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
输出:
[LCI]