Wednesday, January 22, 2014

Parse HTML Table with Jsoup

Hi All,

Let me share with you the java code that helps me to extract data from html table. The code is based on the Jsoup library that you can add it to your project through Maven repository

<dependency>
 <groupId>org.jsoup</groupId>
 <artifactId>jsoup</artifactId>
 <version>1.7.1</version>
  </dependency>





or you add the Jsoup jar into the project build path (JAR available here : http://jsoup.org/download )

package org.tunindex.parser;

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 MainClass {

 public static void main(String[] args) {
  Document doc = null;
  try {
   doc = Jsoup.connect("http://www.bvmt.com.tn/quotes/resume-data.jsp").get();
   
  } catch (IOException e) {
   
   e.printStackTrace();
  }

     for (Element table : doc.select("table")) {
         for (Element row : table.select("tr")) {
             Elements tds = row.select("td");
             if (tds.size() > 6) {
                 System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
             }
         }
     }

 }

} 
 Hope it helps you

Good night :-)