json Google AJAX API - 如何获得 4 个以上的结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4868815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:43:38  来源:igfitidea点击:

Google AJAX API - How do I get more than 4 Results?

imagejsongoogle-api

提问by Jason

Im using the google APIs ajax below to get images for particular search terms. This is being done in a WinFormsapp.

我使用下面的谷歌 API ajax 来获取特定搜索词的图像。这是在WinForms应用程序中完成的。

The below link seems to work, but it only returns 4 results (via JSON)

下面的链接似乎有效,但它只返回 4 个结果(通过 JSON)

Anyone know how to coax more out of it?

有人知道如何哄更多吗?

http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=Apple+Cake

http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=Apple+Cake

Obviously there has to be another parameter to request more or page through the results, but I can't seem to figure it out? Anyone know?

显然必须有另一个参数来请求更多或翻阅结果,但我似乎无法弄清楚?有人知道吗?

回答by rforte

I believe the only way to do that is to make multiple calls to the webservice specifying the 'start' parameter.

我相信唯一的方法是多次调用指定“start”参数的网络服务。

http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=Apple+Cake&start=4

The startparameter is the 0-based index into the search results. So in this example, it would return images 4..7.

start参数是搜索结果中从 0 开始的索引。所以在这个例子中,它会返回图像 4..7。

You can also add the parameter rsz=[1-8]. The default value is 4. That's why you're getting 4 results per request. Here's a link:
http://code.google.com/apis/imagesearch/v1/jsondevguide.html#basic_query

您还可以添加参数rsz=[1-8]。默认值为 4。这就是每个请求获得 4 个结果的原因。这是一个链接:http:
//code.google.com/apis/imagesearch/v1/jsondevguide.html#basic_query

回答by Jonathan Cutrell

For those of you wondering how to do this, there are quite a few ways. One would be to run a looping query based on a certain event. So, for instance...

对于那些想知道如何做到这一点的人,有很多方法。一种是基于某个事件运行循环查询。那么,例如...

var biebresults = [], start = 0;
function getBieb(startNumber){
    $.getJSON("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=Justin%20Bieber&start="+startNumber+"&callback=?", function(results){
        biebresults.push(results.responseData.results);
        if(biebresults.length < 538){
            start = start + 4;
            getBieb(start);
        } else {
            // Do something with your billion bieb images.
        }
    });
}
getBieb(start);

This particular bit of code (using jQuery, btw) will go and grab the first four images of your favorite pop star. From here it counts the number of results and if it isn't enough, it will run getBieb again, except this time with the startNumber argument increased.

这段特殊的代码(使用 jQuery,顺便说一句)将获取您最喜欢的流行歌星的前四张图片。从这里开始计算结果的数量,如果不够,它将再次运行 getBieb,除了这次增加了 startNumber 参数。

回答by defau1t

Google Feed provides an optional method, where you can specify the no. of results that you want to get. If you don't specify this method the default no. of results you get is 4, however to get more no. of feeds you can specify this optional method as follows;

Google Feed 提供了一种可选方法,您可以在其中指定编号。您想要获得的结果。如果您不指定此方法,则默认没有。你得到的结果是 4,但是要得到更多没有。您可以按如下方式指定此可选方法;

feed.setNumEntries(int);

e-g: feed.setNumEntries(16); // will return 16 results.

例如: feed.setNumEntries(16); // will return 16 results.

.setNumEntries(num) sets the number of feed entries loaded by this feed to num. By default, the Feed class loads four entries.

.setNumEntries() has no return value.

.setNumEntries(num) 将此提要加载的提要条目数设置为 num。默认情况下,Feed 类加载四个条目。

.setNumEntries() 没有返回值。

https://developers.google.com/feed/v1/reference#setNumEntries

https://developers.google.com/feed/v1/reference#setNumEntries

回答by Danish Iqbal

Use the below to get 8 results

使用下面得到8个结果

 $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&start=0&q=".$query; 

After that put the forloop then you well be able to get 64 Results

之后放入for循环然后你就可以得到 64 结果

 <?php

         $search =str_replace(' ', '+', @$_GET["q"]);

            $query = $search;
            for ($i=1; $i < 100; $i+8) { 



            $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&start=".$i."&q=".$query;
            $body = file_get_contents($url);
            $json = json_decode($body);

            for($x=0;$x<count($json->responseData->results);$x++){

            echo "<b>Result ".($x+1)."</b>";
            echo "<br>URL: ";
            ?>
            <a href="<?php echo $json->responseData->results[$x]->url; ?>" target="_blank"> <?php echo $json->responseData->results[$x]->url; ?> </a>
            <?php
            echo "<br>VisibleURL: ";
            ?>
            <a href="http://<?php echo $json->responseData->results[$x]->visibleUrl; ?>" target="_blank"> <?php echo $json->responseData->results[$x]->visibleUrl; ?> </a>
            <?php

            echo "<br>Title: ";
            echo $json->responseData->results[$x]->title;
            echo "<br>Content: ";
            echo $json->responseData->results[$x]->content;
            echo "<br><br>";              

        }
            $i+=8;
         }

?>

?>

回答by vuhoanghiep1993

you can get more if you want

如果你愿意,你可以得到更多

public static void main(String[] args) throws UnsupportedEncodingException, IOException {
String google = "http://www.google.com/search?q=";
String search = "dinh la thang site:dantri.com.vn";
String charset = "UTF-8";
String userAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"; // Change this to your company's name and bot homepage!
System.out.println(URLEncoder.encode(search, charset));
int i=1;
while(i<100){
    Elements links = Jsoup.connect(google + URLEncoder.encode(search, charset)+"&start="+i).userAgent(userAgent).get().select("li.g>h3>a");

    for (Element link : links) {
    String title = link.text();
    //System.out.println(link.toString());
    String url = link.absUrl("href"); // Google returns URLs in format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".
    //System.out.println(url);
    url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");

    if (!url.startsWith("http")) {
        continue; // Ads/news/etc.
    }
    System.out.println(i+"Title: " + title);
    System.out.println("URL: " + url);

    } i=i+10;
    }
}
}

回答by mdromed

Visit... this linkabout method you need: setResultSetSize(num)

访问...这个关于你需要的方法的链接setResultSetSize(num)