2014-11-14 74 views
0

我試圖獲得股票代碼名稱,這是3-4個字母代碼,唯一標識一個股票。以下是我正在嘗試使用的代碼。如何使用java解析HTML頁面中的特定項目?

import java.io.IOException; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class Alpha { 

    public static void main(String[] args) { 

Document doc; 
try { 

    // need http protocol 
    doc = Jsoup.connect("http://www.bloomberg.com/markets/stocks/movers/ftse-100/").get(); 

    // get page title 
    String title = doc.title(); 
    System.out.println("title : " + title); 

    // get all links 
    Elements links = doc.select("a[href="); 
    for (Element link : links) { 

     // get the value from href attribute 
     System.out.println("\nlink : " + link.attr("href")); 
     System.out.println("text : " + link.text()); 

    } 

} catch (IOException e) { 
    e.printStackTrace(); 
} 

但是,而不是獲得所有的鏈接,我想從網頁獲得具體鏈接。例如,數據塊我想之一的HTML代碼:

<tr class="odd"> 
    <td class="first name"> 
     <a href="/quote/AGK:LN">Aggreko PLC</a> 
    </td> 
    <td class="value">1,594.00</td> 
    <td class="change up">+52.00</td>  <td class="delta up">+3.37%</td>  <td class="value">1,561,246</td> 
    <td class="datetime">11:35:00</td> 
    </tr> 

與標籤/報價/ AGK:LN的數據,我想在屏幕上輸出。如何讓程序僅選擇該部分的HTML?

乾杯

回答

0

在cssquery你只需把值 像"a[href='blablbla']"

所以試試這個

Elements links = doc.select("a[href='/quote/AGK:LN']");