使用google custom search api

本文介绍了如何利用Google Custom Search API创建个人网站的搜索功能。首先,需要在Google创建一个自定义搜索引擎,获取CX参数。接着,在Google APIs Console中启用Custom Search API并获取API Key。将这些关键参数用于请求,实现包括网页和图片在内的全面搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值