Jsoup cant extract stock price from the webpage
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
* @th开发者_StackOverflow中文版rows 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 #ltpid
selector is thus sufficient. Your selector tries to find a span which has an ancestor with the class .f22
which has an ancestor with the ID ltpid
.
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.
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Document doc = Jsoup.connect("http://money.rediff.com/companies/selan-exploratio/17020281").get();
String javaScript = doc.select(".m_sectionright script").first().toString();
String regStr = "iValue\\s*=\\s*\\d+\\.?\\d*";
Pattern p = Pattern.compile(regStr);
Matcher matcher = p.matcher(javaScript);
while (matcher.find()) {
System.out.println(matcher.group().replace("iValue = ",""));
break;
}
}
The easiest way is to get it from the javascript block.
精彩评论