如何使用Java从网站中提取HTML表格数据?

时间:2014-11-19 21:46:08

标签: java html jsoup screen-scraping

我有这个网站: www.bloomberg.com

我想提取每家公司的名称和价格;我环顾四周,但我发现以下代码不起作用?

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 testing {
public static void main(String[] args) {


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


    for (Element table : doc.select("table[class=index_members_table dual_border_data_table market_sortable_table alt_rows_stat_table]")) {
         for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
            System.out.println(tds.get(0).text() + "->" + tds.get(1).text());
         }

    }
    } catch (IOException e) {

        e.printStackTrace();
    }

}
}

1 个答案:

答案 0 :(得分:1)

(1)网址无效(有额外方案)(2)<tr>s没有<td>s(例如标题)

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

        for (Element table : doc
                .select("table[class=index_members_table dual_border_data_table market_sortable_table alt_rows_stat_table]")) {
            for (Element row : table.select("tr")) {
                Elements tds = row.select("td");
                if (tds.isEmpty()) { // Header <tr> with only <th>s
                    continue;
                }
        ::::