获取是否存在Google搜索结果的信息(JAVA) [英] Getting information whether a google search results exists or not (JAVA)

查看:81
本文介绍了获取是否存在Google搜索结果的信息(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试解析google以获取搜索结果.我需要的不是搜索结果本身,而是无论是否存在搜索结果,我都需要信息!

i try parsing google for search results. What i need are not the search results themselves, but instead i need the information whether a search result exists or not!

现在我的问题是我想搜索组合的字符串.例如. 最大测试员". 现在,谷歌真的很好,并告诉我: 我们找不到"Max Testperson"的搜索结果,而是"Max Testperson"的搜索结果.但 !!!我不需要Max Testperson,我需要"Max Testperson".

Now my problem is i want to search for combined strings. E.g. "Max Testperson". Now google is really nice and tells me: We could not find search results for "Max Testperson" but instead for Max Testperson. But !!! I do not need Max Testperson, i need "Max Testperson".

所以基本上我对搜索结果本身不感兴趣,而是对搜索结果之前的部分感兴趣(无论是否可以找到搜索字符串!).

So basically i am not interested in the search results themselves, but instead into the part before the search results (Whether a search string can be found or not!).

我在Java中使用了以下教程: http://mph-web.de/使用Java-top-10-google-search-results/

I used the following tutorial in java: http://mph-web.de/web-scraping-with-java-top-10-google-search-results/

我可以解析搜索结果.但是就像我说的!没必要!我只想知道我的搜索字符串是否存在.由于Google删除了->"<-,我还是得到了搜索结果.

With this i can parse the search results. But like i said! No need for that! I just want to know if my search string exists or not. Since google removes the ->" "<- i get search results anyways.

有人可以帮我吗?

推荐答案

尝试将get参数nfpr=1添加到搜索中以禁用自动更正功能:

Try to add the get parameter nfpr=1 to your search to disable the auto-correction feature:

final Document doc = Jsoup.connect("https://google.com/search?q=test"+"&nfpr=1").userAgent(USER_AGENT).get();

更新:

您可以解析有关无结果的消息:

You could parse for the message regarding no result:

public class App {
    public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";

    public static void main(String[] args) throws Exception {

        String searchTerm = "\"daniel+nasseh\"+\"26.02.1987\"";
        boolean hasExactResults = true;

        final Document doc = Jsoup.connect("https://google.com/search?q=" + searchTerm + "&nfpr=1")
                .userAgent(USER_AGENT).get();

        Elements noResultMessage = doc.select("div.e.obp div.med:first-child");

        if (!noResultMessage.isEmpty()) {

            hasExactResults = false;

            for (Element result : noResultMessage) {
                System.out.println(result.text());
            }
        }

        if (hasExactResults) {
            // Traverse the results
            for (Element result : doc.select("h3.r a")) {

                final String title = result.text();
                final String url = result.attr("href");

                System.out.println(title + " -> " + url);
            }
        }
    }
}

更新2 :Donselm自己在评论中提出的最佳解决方案是添加&tbs=li:1以强制搜索确切的搜索词

Update 2: best solution as presented from Donselm himself in the comments is to add &tbs=li:1 to force the search for the exact search term

String searchTerm = "\"daniel+nasseh\"+\"26.02.1987\"";

final Document doc = Jsoup.connect("https://google.com/search?q=" + searchTerm + "&tbs=li:1").userAgent(USER_AGENT).get();

这篇关于获取是否存在Google搜索结果的信息(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆