2017年3月23日星期四

google custom search engine api 自定义搜索引擎api

获取 search engine ID
Google CSE SETUP
获取 api 的 key
Setting up Google API keys
custom search engine api
Using REST to Invoke the CSE API
Google Custom Search JSON API
取下一页内容:start=11&num=10,注意 num<=10
例子
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Tiny;
use JSON;
use Encode;
use Data::Dumper;
# https://www.googleapis.com/customsearch/v1?key=$KEY&cx=$CX&q=$query&fields=items(title,link)
our $API_KEY = 'AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxY';
our $ENGINE_ID = '088888888888888888883:txxxxxxxxxo';
my $d = query_xs( { key => $API_KEY, cx => $ENGINE_ID, q=> '风尘叹', fields=> "items(title,link)"} );
print Dumper($d);
my $d2 = query_xs( { key => $API_KEY, cx => $ENGINE_ID, q=> '风尘叹', fields=> "items(title,link)",
        start => 11,
        num => 10,
    } );
print Dumper($d2);
sub query_xs {
    my ($r) = @_;
    my $s = join("&", map { "$_=$r->{$_}" } keys(%$r));
    my $url = "https://www.googleapis.com/customsearch/v1?$s";
    my $res = HTTP::Tiny->new->get($url);
    return unless $res->{success};
    my $d = decode_json($res->{content});
    return $d;
}

没有评论:

发表评论