【问题标题】:How to get address from json object which gets div from google search如何从从谷歌搜索获取 div 的 json 对象获取地址
【发布时间】:2014-01-06 08:25:55
【问题描述】:

当我在 Google 中输入公司名称,然后按 Enter 键时,它会在右侧显示公司图片和 CEO 的地址等详细信息。

我的 java 代码使用 HTMLUnit 并获取此 div 并转换为 json 对象。在这个 json 对象中,公司的地址是存在的。让我知道如何从 json 输出中获取此地址。它不是标签,它来自谷歌搜索的右侧。

我的代码在这里。

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

【问题讨论】:

  • 请发布完整的 Json 响应。从您的示例中,我们看不到地址或电话。谢谢
  • 嗨 Maxim 感谢您的回复,我尝试输入完整的 json 响应,但它超过 30000 个字符,所以堆栈没有接受它,我的最终目标就像用户时的潜在客户生成工具(我的代码)输入公司名称,它应该在右侧 div 中获取谷歌搜索中存在的详细信息。如何在 java 代码中做到这一点?
  • 只发布与地址/电话相关的部分,与您发布的示例无关
  • 当我们在谷歌搜索中输入任何公司名称并输入时,大多数时候我们会在屏幕右侧获取公司的一些信息,我想捕获这些详细信息。如何在 java 中做到这一点?对不起,我没有得到你问的在 json 对象中发布的内容

标签: java json html htmlunit


【解决方案1】:

您可以如下解析 JSON 对象并获取地址和电话号码

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

while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

【讨论】:

  • 谢谢 Teeg。有效。现在我想提取谷歌评论,我正在使用 java HTML 单元。我无法在谷歌评论后面模拟 javascript onclick。例如在谷歌搜索中输入“vidyarthi bhavan”,在屏幕右侧你会得到sn-p,我会提取除谷歌评论之外的所有信息。问题是当我按下谷歌评论时,它不是一个新页面,它只是一个弹出式的东西。我搜索了很多但没有得到,请让我知道如何使用 java 代码从谷歌中提取评论。
猜你喜欢
  • 2012-08-19
  • 1970-01-01
  • 2014-03-23
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
相关资源
最近更新 更多