2016-12-01 5 views
0

komplette Java und Jsoup Anfänger bekommen und ich bin fest. Ich mache ein Programm, das auf einer Website auf Yahoo Finance schaut und den aktuellen Preis des Aktien- und 52-Wochen-Bereichs in der Tabelle ergreift. Ich habe ein Parsing-Problem mit dem aktuellen Preis. see my browser here Ich versuche, diesen Aktienkurs nach Spannungsklasse zu greifen, und wie Sie in meinem Bild sehen können, ändert sich die Spanne, die den Preis enthält, wenn der Bestand gesunken ist (rot) und wenn der Bestand hoch ist (grün). Wie kann ich diesen Preis in Jsoup bekommen, so dass ich ihn unabhängig von der Klasse auswählen kann?Jsoup Java Html Scraping kann nicht Nummer

Hier ist mein aktueller Code. spice ist der aktuelle Preis im String-Format. langsam ist der 52-Wochen-Bereich im String-Format. Danke im Voraus.

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

Antwort

0

Suchen Sie nach einem benachbarten Elemente mit einer stabilen id und von dort navigieren.

Zum Beispiel:

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

Dank sehr hilfreich – Joeysk