无法获得Google搜索结果

时间:2019-01-22 08:15:59

标签: python google-search

我使用Google自定义搜索api搜索iphone

我获得了api_key和自定义搜索引擎ID。

然后,我遵循google提供的示例。

但是,没有返回结果。

如何获得正确的结果?

我的代码如下:

import requests
import ast

url = r'https://www.googleapis.com/customsearch/v1?key=my_key&cx=se_id&q=iphone'
response = requests.get(url)
result = ast.literal_eval(response.text)

response.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?}&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': {'request': [{'title': 'Google Custom Search - iphone', 'totalResults': '0', 'searchTerms': 'iphone', 'count': 10, 'inputEncoding': 'utf8', 'outputEncoding': 'utf8', 'safe': 'off', 'cx': 'abc'}]}, 'searchInformation': {'searchTime': 0.528123, 'formattedSearchTime': '0.53', 'totalResults': '0', 'formattedTotalResults': '0'}}

response.text

'{\n "kind": "customsearch#search",\n "url": {\n  "type": "application/json",\n  "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"\n },\n "queries": {\n  "request": [\n   {\n    "title": "Google Custom Search - iphone",\n    "totalResults": "0",\n    "searchTerms": "iphone",\n    "count": 10,\n    "inputEncoding": "utf8",\n    "outputEncoding": "utf8",\n    "safe": "off",\n    "cx": "abc"\n   }\n  ]\n },\n "searchInformation": {\n  "searchTime": 0.528123,\n  "formattedSearchTime": "0.53",\n  "totalResults": "0",\n  "formattedTotalResults": "0"\n }\n}\n'

0 个答案:

没有答案