[英]Jsoup Java Html scraping cannot get number
完整的Java和jsoup新手,我被困住了。 我正在制作一個程序,該程序可以查看Yahoo Finance網站上的內容,並可以獲取表格中該股票的當前價格和52周范圍。 我目前的價格有解析問題。 在這里查看我的瀏覽器,我正在嘗試按跨度類別獲取股票價格,正如您在我的圖像中看到的那樣,跨度包含當股票下跌(紅色)和股票上漲(綠色)時價格的變化。 我該如何在Jsoup中獲取該價格,以便無論其類別如何都可以選擇它?
這是我當前的代碼。 sprice是字符串格式的當前價格。 慢是字符串格式的52周范圍。 提前致謝。
Document doc = Jsoup.connect("http://finance.yahoo.com/quote/AAPL? ltr=1").timeout(10*1000).get();
Elements spans = doc.select("span");
Element span = null;
Elements rows = doc.select("td");
Element row = null;
double price= 0;
double low = 0;
String sprice = "";
String slow = "";
if (spans.hasClass("Fw(b) D(ib) Fz(36px) Mb(-4px)")) {//*this code gets the current price on yahoo.com
span = spans.get(13);
sprice = span.text();
System.out.println("the sprice is: " + sprice);
}
if(rows.hasClass("Ta(end) Fw(b)")){//*this code gets the 52 week range on yahoo.com
row = rows.get(13);
slow =row.text();
System.out.println("the slow is: " + slow);
}
查找具有穩定id
的相鄰元素,然后從那里導航。
例如:
doc.getElementById("quote-market-notice").parent().child(0).text()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.