簡體   English   中英

Jsoup 無法從網頁中提取股票價格

[英]Jsoup cant extract stock price from the webpage

我一直在使用 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);
    }
}

股票價格似乎存儲在 ID ltpid的跨度中。 因此,使用#ltpid選擇器就足夠了。 您的選擇器嘗試查找具有 class .f22的祖先的跨度,其祖先的 ID ltpid

閱讀http://jsoup.org/apidocs/org/jsoup/select/Selector.html了解有關選擇器的說明。

編輯:

但是,您還有第二個問題:此跨度不在您加載的文檔中。 It's inside an iframe which has the following URL: http://money.rediff.com/money1/current_stat.php?companyCode=17020281 .

試試這個 URL 而不是你正在使用的那個,它會工作的。

     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 塊中獲取它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM