gpt4 book ai didi

java - 在 Java 中通过 Google Custom Search API V1 获取超过 10 个结果

转载 作者:太空狗 更新时间:2023-10-29 22:53:15 29 4
gpt4 key购买 nike

我在 Java 中使用 Google Custom Search API 来获取 Google 响应查询的结果。这段代码是我借助其他帖子写的,代码如下:

    url = new URL("https://www.googleapis.com/customsearch/v1?key="+key+ "&cx="+ cx +"&q="+    searchText+"&alt=json"+"&start="+0+"&num="+30);
HttpURLConnection conn2 = (HttpURLConnection) url.openConnection();
System.out.println("Connection opened!");
conn2.setRequestMethod("GET");
conn2.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn2.getInputStream())));

问题是,每当我使用上面没有 num 和 start 参数的代码时,它都能正常执行,但只给出前 10 个结果。所以我使用了 num 和 start 参数。但他们正在制造问题。在这里我无法理解将 numstart 参数放在 url 中的什么位置。它总是给出 HTTP 400,即错误请求。我已经阅读了文档页面,也没有明确说明将这两个参数放在 Url 中的位置。

所以如果有人帮助我解决这个问题,我将非常感激。谢谢。

最佳答案

你不能那样做。 num 最多只能是 10。参见

https://developers.google.com/custom-search/json-api/v1/reference/cse/list#num

num - unsigned integer
Number of search results to return. Valid values are integers between 1 and 10, inclusive.

为了显示更多结果,Google 建议进行多次调用,根据需要增加 start 参数:

https://developers.google.com/custom-search/json-api/v1/reference/cse/list#start

start - unsigned integer The index of the first result to return. Valid value are integers starting 1 (default) and the second result is 2 and so forth. For example &start=11 gives the second page of results with the default "num" value of 10 results per page. Note: No more than 100 results will ever be returned for any query with JSON API, even if more than 100 documents match the query, so setting (start + num) to more than 100 will produce an error. Note that the maximum value for num is 10.

关于java - 在 Java 中通过 Google Custom Search API V1 获取超过 10 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16925762/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com