Jsoup无法从网页中提取股票价格
我一直在使用 Jsoup 从股票交易网站提取股票价格。股票价格定期自动更新。我尝试过使用食谱中给出的示例,但没有任何运气,请帮助我......
以下是我尝试过的......
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
股票价格似乎存储在 ID 为
ltpid
的范围内。因此,使用#ltpid 选择器就足够了。您的选择器尝试查找一个具有.f22
类祖先的跨度,该类的祖先 ID 为ltpid
。阅读http://jsoup.org/apidocs/org/jsoup/select/Selector。 html 有关选择器的解释。
编辑:
不过,您还有第二个问题:此跨度不在您加载的文档内。它位于具有以下 URL 的 iframe 内: http://money.rediff.com /money1/current_stat.php?companyCode=17020281。
尝试使用此 URL 而不是您正在使用的 URL,它会起作用。
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 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.
最简单的方法是从 javascript 块中获取它。
The easiest way is to get it from the javascript block.