当我在Google中输入公司名称并按回车键时,它会给出公司形象的右侧,并解决CEO等详细信息。
我的java代码使用HTMLUnit
并获取此div并转换为json对象。在这个json公司的对象地址存在。让我知道如何从json输出中获取此地址。它不是TAG,它来自谷歌搜索的右侧。
我的代码在这里。
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();
}
}
}
这个在json对象中的输出就像这样,我想得到这个json对象中存在的地址和电话号码。这该怎么做。
"span":{"content":[":",", Raj Bhavan Road,",", Karnataka -Get contact address
答案 0 :(得分:0)
您可以解析JSON对象,如下所示,并获取地址和电话号码
JSONArray msg = (JSONArray) xmlJSONObj.get("content");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}