HTML table id and class id


如何在

以下网址中找到大表的表ID:http://en.wikipedia.org/wiki/States_and_territories_of_India

我能够看到课程wikitable sortable jquery-tablesorter

这是包含印度各州列表的表格。我能够从 firebug 确认此表 = wikitable sortable jquery-tablesorter 具有状态列表。如何获取该表的 ID?

获取该表中的所有名称的 CSS 等效项是什么?

我只想得到状态...第一列。我正在使用jsoup。

如果这仍然是悬而未决的问题,以下是获取印度各州列表的方法:

public static void main(String[] args) throws IOException
    {
        Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/States_and_territories_of_India").get();
        Elements tables = doc.select("table");
        for (Element table : tables) {
            Element tableCaption = table.getElementsByTag("big").first();
            if (tableCaption != null && tableCaption.text().equals("States of India")) {
                Document statesDoc = Jsoup.parse(table.toString());
                Elements states = statesDoc.select("tr td:eq(0)");
                for (Element state : states) {
                    System.out.println(state.text().replaceAll("\[\d\]", ""));
                }
            }
        }
    }

该表上没有 ID。如果你想获取具有类"wikitable"的表的内容。将 Jsoup 与这段代码一起使用

package com.main;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Main {
    public static void main (String args[]){
        Document doc;
        try {
            doc = Jsoup.connect("http://en.wikipedia.org/wiki/States_and_territories_of_India").get();
            Elements newsHeadlines = doc.select("table.wikitable").get(0).select("td:eq(0) a");
            System.out.println(newsHeadlines.html());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

所以看起来你正在尝试截屏这个表。

您的问题的答案是该特定<table>没有id

启动表的 html 是:

<table class="wikitable sortable jquery-tablesorter" style="width:70%;">

如您所见,该元素没有id属性。

您使用哪些库来解析 HTML?在 JavaScript 中,您可以使用 document.getElementsByClassName('wikitable')[0] 并在页面上找到唯一的它。但是您将使用的语法将取决于您可以使用哪种 HTML DOM 遍历。

id 元素是可选的;并非页面上的每个元素都有一个。此表没有。

Using JQuery.你想要第一个带有类的表维基表可排序jquery-table-sorter。

$(".wikitable.sortable.jquery-table-sorter").first()

虽然,css 类可以随时更改,所以我不会依赖它。可能值得要求可以编辑 wiki 页面的人向所有表添加 ID。

最新更新