没有结果来自Google自定义搜索

时间:2019-01-09 21:37:58

标签: python google-api google-custom-search google-api-python-client

我正在尝试使用具有API KEY和CSE代码的Python在Google API中创建自定义搜索。 搜索完成(代码:200),但未收到任何结果。 有人可以帮助我解决这个问题吗?

这是我的代码:

from googleapiclient.discovery import build
import pprint

my_api_key = "XXX"
my_cse_id = "XXX"

def google_search(search_term, api_key, cse_id, **kwargs):
    service = build("customsearch", "v1", developerKey=api_key)
    res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
    return res['items']

results = google_search('usa',my_api_key,my_cse_id,num=1)

print(results)

这是我的决心:

 'queries': {'request': [{'count': 10,
                      'cx': 'xxx',
                      'inputEncoding': 'utf8',
                      'outputEncoding': 'utf8',
                      'safe': 'off',
                      'searchTerms': 'lectures',
                      'title': 'Google Custom Search - lectures',
                      'totalResults': '0'}]},
 'searchInformation': {'formattedSearchTime': '0.11',
                   'formattedTotalResults': '0',
                   'searchTime': 0.108816,
                   'totalResults': '0'},
 'url': {'template': 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&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',
     'type': 'application/json'}}

谢谢!

0 个答案:

没有答案