(Java)获取google的前n个结果作为链接

时间:2018-07-09 20:29:03

标签: java api google-search

首先,我寻找了类似的问题,但找不到所需的答案。因此,请问这个问题不是唯一的新问题。

我想获取Google 链接的前N个(可能是5个或10个)结果。 此刻我有这样的东西:

String url="http://www.google.com/search?q=";
String charset="UTF-8";
String key="java";
String query = String.format("%s",URLEncoder.encode(key, charset));
URLConnection con = new URL(url+ query).openConnection();
//next line is to trick Google who is blocking the default UserAgent
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
in.close();

这为我提供了此搜索的完整Google html代码,但我只想获取前n个结果的原始链接。我该如何处理?

谢谢。

2 个答案:

答案 0 :(得分:2)

我已经进行了一些html调查,您必须在字符串中搜索:

<h3 class="r"><a href="/url?q=

此后,出现一个链接,该链接继续双引号。我会尽快编写脚本。
编辑
在Google中搜索字符串键时,应该会获得前n个链接:

public static String[] getLinks(String key, int n) throws MalformedURLException, IOException {
    String url = "http://www.google.com/search?q=";
    String charset = "UTF-8";
    String query = String.format("%s", URLEncoder.encode(key, charset));
    URLConnection con = new URL(url + query).openConnection();
    con.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    String wholeThing = "";
    while ((inputLine = in.readLine()) != null) wholeThing += inputLine;
    in.close();

    List<String> strings = new ArrayList<String>();
    String search = "<h3 class=\"r\"><a href=\"/url?q=";
    int stringsFound = 0;
    int searchChar = search.length();
    while(stringsFound < n && searchChar <= wholeThing.length()) {
        if(wholeThing.substring(searchChar - search.length(), searchChar).equals(search)) {
            int endSearch = 0;
            while(!wholeThing.substring(searchChar + endSearch, searchChar + endSearch + 4).equals("&amp")) {
                endSearch++;
            }
            strings.add(wholeThing.substring(searchChar, searchChar + endSearch));
            stringsFound++;
        }
        searchChar++;
    }
    String[] out = new String[strings.size()];
    for(int i = 0; i < strings.size(); i++) {
        out[i] = strings.get(i);
    }
    return out;
}

请确保导入java.util.list,而不是java.awt.list!

答案 1 :(得分:1)

您可能想尝试jsoup库,因为它需要花费大量精力来解析网页:

Elements links = Jsoup.connect("https://www.google.com.au/search?q=fred")
    .get().select("h3.r").select("a");
for (Element link : links)
    System.out.println(link);

Elements扩展了ArrayList<Element>,因此您可以使用以下命令访问前n个元素:

for (int i = 0; i < n; i++)
    System.out.println(links.get(i));

或者,使用流:

links.stream().limit(n)...

如果您只想要原始网址:

link.attr("href")

因此,将所有内容放在一起,以下内容将为Google搜索“ fred”一词打印前5个原始链接:

Jsoup.connect("https://www.google.com.au/search?q=fred").get()
    .select("h3.r").select("a")
    .stream()
    .limit(5)
    .map(l -> l.attr("href"))
    .forEach(System.out::println);