在"__init__.py"Google 自定义搜索 API 中找不到引用"发现"



在遵循本教程时:https://www.youtube.com/watch?v=IBhdLRheKyM

我遇到了一个问题,我已经尝试了几件事,比如用googleapiclient.discovery替换apiclient.discovery。 没用。

type(resource( 和 len(result['items'] 返回 void。 PyCharm 指出存在未解析的引用。

Cannot find reference 'discovery' in '__init__.py'

我使用 pip 工具安装了模块。(pip install google-api-python-client(,这没有任何问题。

示例代码:

from apiclient.discovery import build
api_key = "api_key"
resource = build("customsearch", 'v1', developerKey=api_key).cse()
result = resource.list(q='alfa romeo', cx="searchengineID").execute()
type(resource)
len(result['items'])
>> Process finished with exit code 0

编辑了api_key和 CX,因为它是敏感信息。

">初始化.py"来源:

"""Retain apiclient as an alias for googleapiclient."""
from six import iteritems
import googleapiclient
from googleapiclient import channel
from googleapiclient import discovery
from googleapiclient import errors
from googleapiclient import http
from googleapiclient import mimeparse
from googleapiclient import model
try:
from googleapiclient import sample_tools
except ImportError:
# Silently ignore, because the vast majority of consumers won't use it and
# it has deep dependence on oauth2client, an optional dependency.
sample_tools = None
from googleapiclient import schema
__version__ = googleapiclient.__version__
_SUBMODULES = {
'channel': channel,
'discovery': discovery,
'errors': errors,
'http': http,
'mimeparse': mimeparse,
'model': model,
'sample_tools': sample_tools,
'schema': schema,
}
import sys
for module_name, module in iteritems(_SUBMODULES):
sys.modules['apiclient.%s' % module_name] = module

嗯,将确切的代码(替换API密钥和CX(粘贴到shell中对我有用,在Python 2.7和3.5中都对我有用。

通常,如果apiclient.discovery.build()失败,它将引发异常,而不是返回无效值。

当你说"类型(资源(和len(result['items']返回无效"时,你是什么意思?由于 void 不是 Python 中的一种类型,你的意思是<class 'NoneType'>吗?

另外,type(result)返回什么?

相关内容

最新更新