在遵循本教程的同时:https://www.youtube.com/watch?v=IBhdLRheKyM
我遇到了问题,并且尝试了多种方法,例如将apiclient.discovery替换为googleapiclient.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作为敏感信息。
' init .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
答案 0 :(得分:1)
嗯。在Python 2.7和3.5中,将确切的代码(替换API密钥和CX)粘贴对我来说是有效的。
通常,如果apiclient.discovery.build()
失败,它将引发异常,而不是返回无效值。
当您说“ type(resource)和len(result ['items']返回void”时,您是什么意思?由于void不是Python中的类型,您是说<class 'NoneType'>
吗?
另外,type(result)
返回什么?