Jsoup无法从网页中提取股票价格

发布于 2024-11-18 02:15:57 字数 649 浏览 1 评论 0原文

我一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就此别过 2024-11-25 02:15:57

股票价格似乎存储在 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 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.

傲世九天 2024-11-25 02:15:57
     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;
        }
    }

最简单的方法是从 javascript 块中获取它。

     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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文