2014-11-14 39 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']");