您的位置:首页 > 编程语言 > Go语言

使用google custom search api

2013-05-08 19:32 344 查看
google的search api非常强大也非常好用,它提供一个custom search api功能能快速地实现个人用户网站的搜索功能,同时它也能提供对整个网站的搜索,不仅包括文字还包括图片的搜索。


要使用这个api包括2个部分,首先要创建一个自己的搜索引擎,前提是你已经有google的账号。

1.去http://www.google.com/cse/manage/create,填写一个网站的名称,这个网站就是你的个人网站,然后点击创建,会得到一段javascript的代码,把这段代码放到你的网站上就能实现google的搜索功能,特别要注意的是,这个搜索功能默认只在你提供的网站里搜索,需要到https://www.google.com/cse/all里面设置为搜索全局网络和图片搜索功能。最后生成一个cx参数,这个十分重要,是搜索引擎的ID,以后需要用到。



2.然后访问https://code.google.com/apis/console,并在Services面板中打开JSON/Atom
Custom Search API选项;接着我们去API Access面板,得到我们的API key,之后我们可以在请求参数中使用key=yourAPIKey。

这2步完成以后,我们就可以通过http来提交搜索请求,有很多可选的参数,参考https://developers.google.com/custom-search/v1/cse/list#try-it

比如我要搜索是建筑物的图片: https://www.googleapis.com/customsearch/v1?key=yourKey&cx=Yourcx&q=building&searchType=image
返回的结果为:

{
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
},
"queries": {
"nextPage": [
{
"title": "Google Custom Search - building",
"totalResults": "60040000000",
"searchTerms": "building",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx":
"searchType": "image"
}
],
"request": [
{
"title": "Google Custom Search - building",
"totalResults": "60040000000",
"searchTerms": "building",
"count": 10,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx":
"searchType": "image"
}
]
},
"context": {
"title":
},
"searchInformation": {
"searchTime": 0.258324,
"formattedSearchTime": "0.26",
"totalResults": "60040000000",
"formattedTotalResults": "60,040,000,000"
},
"items": [
{
"kind": "customsearch#result",
"title": "Building Authority",
"htmlTitle": "\u003cb\u003eBuilding\u003c/b\u003e Authority",
"link": "http://www.baycounty-mi.gov/Images/BuildingAuthority/CountyBuilding.jpg",
"displayLink": "www.baycounty-mi.gov",
"snippet": "the Bay County Building,",
"htmlSnippet": "the Bay County \u003cb\u003eBuilding\u003c/b\u003e,",
"mime": "image/jpeg",
"image": {
"contextLink": "http://www.baycounty-mi.gov/BuildingAuthority/",
"height": 1278,
"width": 1704,
"byteSize": 618685,
"thumbnailLink": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRO2r_euLgonFl1B4vv6DZBtRKXvbPSSrhMNg7E0ogo1saJxP2IDr6qZvqf",
"thumbnailHeight": 112,
"thumbnailWidth": 150
}
},
....
}
]
}


其中包含了很多图片的url,我们可以直接根据这些url批量下载图片,注意到这次返回结果为10,我们可以设置返回结果数目(最大是10.....),以及访问设置start(最大是91)来得到更多的结果(可惜好像只能获取100个结果),用户每天的查询量为100次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: