简体   繁体   中英

Jsoup Java Html scraping cannot get number

complete java and jsoup novice and I am stuck. I am making a program that looks at a website on yahoo finance and grabs the current price of the stock and 52 week range in the table. I am having a parsing issue with the current price. see my browser here I am trying to grab that stock price by span class and as you can see in my image, the span containing the price changes when the stock is down (red) and when the stock is up (green). How can I grab that price in Jsoup so that I can select it regardless of class?

Here is my current code. sprice is the current price in string format. slow is the 52 week range in string format. Thanks in advance.

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);
    }

Look for a neighbouring element with a stable id , and navigate from there.

For example:

doc.getElementById("quote-market-notice").parent().child(0).text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM