I have been using Jsoup to extract a a stock price from a stock trading website. The stock price is updated automatically at regular intervals. I have tried using the examples given in the cookbook,,but have not been having any luck please help me out…
The following is what i have tried…
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class sup {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String url="http://money.rediff.com/companies/selan-exploratio/17020281";
Document doc = Jsoup.connect(url).get();
String quote = doc.select("#ltpid .f22 span").first().text();
System.out.println(quote);
}
}
The stock price seems to be stored in a span having the ID
ltpid. Using the#ltpidselector is thus sufficient. Your selector tries to find a span which has an ancestor with the class.f22which has an ancestor with the IDltpid.Read http://jsoup.org/apidocs/org/jsoup/select/Selector.html for explanations about selectors.
EDIT:
You have a second problem though: this span is not inside the document you have loaded. It’s inside an iframe which has the following URL: http://money.rediff.com/money1/current_stat.php?companyCode=17020281.
Try with this URL instead of the one you’re using, and it’ll work.