Thursday, January 6, 2011

Awesome HtmlUnit

I grabbed (scraping) more than 100 international web sites which include both the job portal and for RRS Feed data in my last company. I used the open source Html Parser for this purpose.this is very interesting library i parse many websites which include search result like Google.
Today i show most interesting API example which is used for scrapping and testing. i feel this is the ever best scrapping API because of providing full control with html from code.i am talking about HtmlUnit.
In the following example i show you how i can parse the data before providing the search criteria.
Suppose you have some web site which is show you first Html for including text Fields,html Select,check boxes etc. you need to first enter your input and when received result parse all the records.let suppose you have the following web sites.

Step1: download the htmlUnit library and add in your project.
I only show you how you can submit the html Form and receive the result of pages after setting the search criteria on first page.

Step:2 use the below code which first input the html form and submit and receive the results.



import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
/**
 *
 * @author noman.sadiq
 *
 */
public class UniTest{
protected HtmlPage currentPage;
   
       
    public static void main(String[] args) {
        try{
            WebClient webClient = new WebClient();
            ProxyConfig pc=new ProxyConfig();
            pc.setProxyHost("192.168.100.16");
            pc.setProxyPort(8080);
            webClient.setProxyConfig(pc);
           
            HtmlPage currentPage = webClient.getPage("http://www.fsa.gov.uk/register/firmSearchForm.do");
        //    System.out.println(currentPage.asXml());
            System.out.println("current URL: "+currentPage.getUrl());

            // setting the Firm name
            HtmlInput queryInput = currentPage.getElementByName("firmName");
            queryInput.setValueAttribute("AXA");
            // select search type
            HtmlSelect select= currentPage.getElementByName("searchType");
            select.setSelectedAttribute("2", true);
           
            // set authorized true
            HtmlCheckBoxInput checkbox= currentPage.getElementByName("currAuthorisedInd");
            checkbox.setChecked(true);
           
            // submit the form
            HtmlSubmitInput submitBtn = currentPage.getElementByName("Submit");
            currentPage = submitBtn.click();
           
            System.out.println(currentPage.asXml());

//  now parse the result like using the anchor Tag or table or result what ever.
           
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}


when you received the result on console save it as html page,when you open you will see the result page look like that
so you successfully submit the html form after fill the search criteria now you can parse the required data. you can follow the link and also scrap the data on next level.

This is very powerful library for testing and scrapping in Java enjoy! it.

No comments:

Post a Comment