3

Jsoup を使用して、この Web サイトから行ごとに個別に仕様データを抽出するにはどうすればよいでしょうか。たとえば、Network->Network Type、Battery などです。

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

public class mobilereviews {
    public static void main(String[] args) throws Exception {
        Document doc = Jsoup.connect("http://mobilereviews.net/details-for-Motorola%20L7.htm").get();
        for (Element table : doc.select("table")) {
            for (Element row : table.select("tr")) {
                Elements tds = row.select("td");
                System.out.println(tds.get(0).text());   
            }
        }
    }
}
4

4 に答える 4

6

ここにあなたの問題の解決策を見つける試みがあります

Document doc = Jsoup.connect("http://mobilereviews.net/details-for-Motorola%20L7.htm").get();

for (Element table : doc.select("table[id=phone_details]")) {
     for (Element row : table.select("tr:gt(2)")) {
        Elements tds = row.select("td:not([rowspan])");
        System.out.println(tds.get(0).text() + "->" + tds.get(1).text());
     }
}

HTML の解析は複雑で、HTML が変更された場合はコードも変更する必要があります。

最初に HTML マークアップを調べて、解析規則を考え出す必要があります。

  • HTML には複数のテーブルがあるため、最初に正しいテーブルでフィルタリングしますtable[id=phone_details]
  • 表の最初の 2 行には書式設定用のマークアップのみが含まれているため、スキップしてくださいtr:gt(2)
  • 1 行おきにコンテンツ タイプのグローバルな説明で始まり、フィルターで除外します。td:not([rowspan])

セレクタ構文のより複雑なオプションについては、こちらを参照してくださいhttp://jsoup.org/cookbook/extracting-data/selector-syntax

于 2013-04-06T17:46:20.277 に答える
3

列の xpath -//*[@id="phone_details"]/tbody/tr[3]/td[2]/strong

値の xpath -//*[@id="phone_details"]/tbody/tr[3]/td[3]

@Joeyのコードは、これらに焦点を合わせようとします。select()Xpath に基づいてルールを記述できるはずです。

数値 (tr[N] / td[N]) を適切な値に置き換えます。

または、HTML をテキストのみのブラウザーにパイプして、テキストからデータを抽出することもできます。テキスト版のページはこちら。テキストを区切るか、N 文字の後に読み取ってデータを抽出できます。

于 2013-04-06T18:05:11.110 に答える
1

JSoup を介して 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 ExtractTableDataUsingJSoup {

    public static void main(String[] args) {
        extractTableUsingJsoup("http://mobilereviews.net/details-for-Motorola%20L7.htm","phone_details");
    }

    public static void extractTableUsingJsoup(String url, String tableId){
        Document doc;
        try {
            // need http protocol
            doc = Jsoup.connect(url).get();

            //Set id of any table from any website and the below code will print the contents of the table.
            //Set the extracted data in appropriate data structures and use them for further processing
            Element table = doc.getElementById(tableId);

            Elements tds = table.getElementsByTag("td");

            //You can check for nesting of tds if such structure exists
            for (Element td : tds) {
                System.out.println("\n"+td.text());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2014-12-17T05:33:45.900 に答える
1

これは、html テーブルからデータを取得する方法です。

org.jsoup.nodes.Element tablaRegistros = doc
                    .getElementById("tableId");
for (org.jsoup.nodes.Element row : tablaRegistros.select("tr")) {
                for (org.jsoup.nodes.Element column : row.select("td")) {
                    // Elements tds = row.select("td");
                    // cadena += tds.get(0).text() + "->" +
                    // tds.get(1).text()
                    // + " \n";
                    cadena += column.text() + ",";
                }
                cadena += "\n";
            }
于 2013-11-05T03:20:02.097 に答える