Question

when i type a company name in Google, and press enter, it will give a right side of image of the company and address CEO etc details.

My java code uses HTMLUnit and gets this div and is converted to json object. In this json object address of the company is present. Let let me know how to get this address from the json output. It is not a TAG, it has come from right side of Google search.

My code is here.

import org.json.JSONObject;
import org.json.XML;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;


public class googleHTMLUnit
{

    public static final String TEST_XML_STRING = null;
    public static final int PRETTY_PRINT_INDENT_FACTOR = 0;


    public void homePage_Firefox() throws Exception
    {
        final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
        String searchparameter = "jn planetarium bangalore";
        HtmlPage page = webClient.getPage("https://www.google.co.in/search?output=search&sclient=psy-ab&q=" + searchparameter  + "&btnG=");
        DomNodeList<DomElement> button = page.getElementsByTagName("a");
        HtmlPage page2 = null;


        for(int i = 0; i < button.size(); i++ )
        {
            //System.out.println(button.get(i).getTextContent());
            if(button.get(i).getTextContent().contains("Google review"))
            {
                //System.out.println(button.get(i).getTextContent());
                Iterable<HtmlElement> buttontobeclicked = button.get(i).getHtmlElementDescendants();
                for(HtmlElement test:buttontobeclicked)
                {
                    System.out.println(test.getNodeValue());
                    System.out.println("inside for ");
                    page2 = test.click();
                    //page2 = test.click();
                    //Event test1 = new Event();
                    //page2 = test.fireEvent(test1);
                    //Object page3 = page2.getJavaScriptResult();
                    //page3.toString();
                    break;
                }
            }
            //System.out.println("href " + button.get(i));
        }
        //System.out.println("href " + button);
        //button.click();
        //System.out.println("After click " + page2.asXml());
        webClient.closeAllWindows();
        //System.out.println(page2.asXml());
        JSONObject xmlJSONObj = XML.toJSONObject(page.asXml());
        System.out.println(xmlJSONObj.toString());

    }




    public static void main(String[] args)
    {
        try 
        {
            //System.setErr(new PrintStream(new File("C:/Users/Desktop/output-file.html")));
            //System.setOut(new PrintStream(new File("C:/Users/Desktop/output-file.html")));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        googleHTMLUnit test = new googleHTMLUnit();
        try
        {
            test.homePage_Firefox();
        } 
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }
}

The output of this will be like this in json object, I want to get address and phone number which is present in this json object. How to do this.

"span":{"content":[":",", Raj Bhavan Road,",", Karnataka -Get contact address
Was it helpful?

Solution

You can parse the JSON object as below and fetch the address and phone number

JSONArray msg = (JSONArray) xmlJSONObj.get("content");
Iterator<String> iterator = msg.iterator();

while (iterator.hasNext()) {
    System.out.println(iterator.next());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top