Skip to content Skip to sidebar Skip to footer

Html Table Id And Class Id

How can I find the Table id of the large table on in the following url: http://en.wikipedia.org/wiki/States_and_territories_of_India I was able to see the classes wikitable sortabl

Solution 1:

If this is still pending issue, here is how you can get list of states in India :

publicstaticvoidmain(String[] args)throws IOException
    {

        Documentdoc= Jsoup.connect("http://en.wikipedia.org/wiki/States_and_territories_of_India").get();

        Elementstables= doc.select("table");

        for (Element table : tables) {
            ElementtableCaption= table.getElementsByTag("big").first();
            if (tableCaption != null && tableCaption.text().equals("States of India")) {
                DocumentstatesDoc= Jsoup.parse(table.toString());
                Elementsstates= statesDoc.select("tr td:eq(0)");
                for (Element state : states) {
                    System.out.println(state.text().replaceAll("\\[\\d\\]", ""));
                }
            }
        }

    }

Solution 2:

There is no ID on that table. If you want to get the content of the table which has the class "wikitable". Use Jsoup with this code

package com.main;

import java.io.IOException;

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

publicclassMain {
    publicstaticvoidmain(String args[]){
        Document doc;
        try {

            doc = Jsoup.connect("http://en.wikipedia.org/wiki/States_and_territories_of_India").get();
            ElementsnewsHeadlines= 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();
        }

    }
}

Solution 3:

So it looks like you're trying to screenscrape this table.

The answer to your question is there there is no id on that particular <table>.

The html that starts the table is:

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

As you can see there is no id attribute for that element.

What libraries are you using to parse the HTML? In JavaScript you could use document.getElementsByClassName('wikitable')[0] and find that uniquely on the page. But the syntax you would use will depend on what kind of HTML DOM traversing are available to you.

Solution 4:

The id element is optional; not every element on a page will have one. This table doesn't.

Solution 5:

Using JQuery. You want the first table with classes wikitable sortable jquery-table-sorter.

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

Although, the css classes could change at any time so I wouldn't rely on that. It might be worth asking someone who can edit the wiki page to add an id to all the tables.

Post a Comment for "Html Table Id And Class Id"