Google自定义搜索JSON API

Rap*_*tor 3 json google-custom-search

我正在使用自定义搜索引擎(CSE)通过JSON API玩Google Custom Search API.我成功地获得了搜索结果,但我对如何获得搜索结果一无所知nextPageToken.

https://www.googleapis.com/customsearch/v1?key=MY_API_KEY&cx=MY_SEARCH_ENGINE_ID&q=Testing

JSON响应如下:

{
 "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 - Testing",
    "totalResults": "2900",
    "searchTerms": "Testing",
    "count": 10,
    "startIndex": 11,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "MY_SEARCH_ENGINE_ID"
   }
  ],
  "request": [
   {
    "title": "Google Custom Search - Testing",
    "totalResults": "2900",
    "searchTerms": "Testing",
    "count": 10,
    "startIndex": 1,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "MY_SEARCH_ENGINE_ID"
   }
  ]
 },
 "context": {
  "title": "Test Search Engine"
 },
 "searchInformation": {
  "searchTime": 0.299265,
  "formattedSearchTime": "0.30",
  "totalResults": "2900",
  "formattedTotalResults": "2,900"
 },
 "items": [
  // ... Search Result here
 ]
}
Run Code Online (Sandbox Code Playgroud)

但是如何通过nextPageToken?获得下一页的结果?

Rap*_*tor 8

而不是使用nextPageToken,我可以添加2个参数到查询字符串中来更改结果页面:

  • start:结果的起始索引,有效值为整数> 0.
  • num:每页结果数,有效值为1~10(1页最多10条记录)

因此,要更改为第2页,我必须发出:

start=11&num=10
Run Code Online (Sandbox Code Playgroud)

在查询字符串中(假设每页的记录数为10).

希望这可以帮助.