【问题标题】:Unable to find the server at www.googleapis.com在 www.googleapis.com 上找不到服务器
【发布时间】:2020-11-07 15:31:17
【问题描述】:

我想用谷歌客户端 API 提取数据。

我已完成的步骤:

  1. 创建了 google 自定义搜索引擎并拥有搜索引擎 ID。
  2. 在一个项目下(在我的谷歌云中)创建了一个搜索 API。

我正在使用这个代码sn-p:

my_api_key = "my_API_KEY"
my_cse_id = "my_CSE ID"

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

results = google_search('yoga', my_api_key, my_cse_id, num=10)
print(results)

也参考了这个帖子:

Programmatically searching google in Python using custom search

我正在获取请求的 URL:

2020-11-07 15:25:54,336 | INFO | discovery.py:272 | _retrieve_discovery_doc | URL being requested: GET https://www.googleapis.com/discovery/v1/apis/customsearch/v1/rest?key=

问题

gaierror                                  Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/httplib2/__init__.py in _conn_request(self, conn, request_uri, method, body, headers)
   1500                 if conn.sock is None:
-> 1501                     conn.connect()
   1502                 conn.request(method, request_uri, body, headers)

/opt/conda/lib/python3.6/site-packages/httplib2/__init__.py in connect(self)
   1269 
-> 1270         address_info = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
   1271         for family, socktype, proto, canonname, sockaddr in address_info:

/opt/conda/lib/python3.6/socket.py in getaddrinfo(host, port, family, type, proto, flags)
    744     addrlist = []
--> 745     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    746         af, socktype, proto, canonname, sa = res

gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

ServerNotFoundError                       Traceback (most recent call last)
<ipython-input-3-b59229e204c0> in <module>()
      9     return res['items']
     10 
---> 11 results = google_search('yoga', my_api_key, my_cse_id, num=10)
     12 results

<ipython-input-3-b59229e204c0> in google_search(search_term, api_key, cse_id, **kwargs)
      5 
      6 def google_search(search_term, api_key, cse_id, **kwargs):
----> 7     service = build("customsearch", "v1", developerKey=api_key, cache_discovery=False)
      8     res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
      9     return res['items']

/opt/conda/lib/python3.6/site-packages/googleapiclient/_helpers.py in positional_wrapper(*args, **kwargs)
    128                 elif positional_parameters_enforcement == POSITIONAL_WARNING:
    129                     logger.warning(message)
--> 130             return wrapped(*args, **kwargs)
    131         return positional_wrapper
    132 

/opt/conda/lib/python3.6/site-packages/googleapiclient/discovery.py in build(serviceName, version, http, discoveryServiceUrl, developerKey, model, requestBuilder, credentials, cache_discovery, cache)
    222     try:
    223       content = _retrieve_discovery_doc(
--> 224         requested_url, discovery_http, cache_discovery, cache, developerKey)
    225       return build_from_document(content, base=discovery_url, http=http,
    226           developerKey=developerKey, model=model, requestBuilder=requestBuilder,

/opt/conda/lib/python3.6/site-packages/googleapiclient/discovery.py in _retrieve_discovery_doc(url, http, cache_discovery, cache, developerKey)
    272   logger.info('URL being requested: GET %s', actual_url)
    273 
--> 274   resp, content = http.request(actual_url)
    275 
    276   if resp.status >= 400:

/opt/conda/lib/python3.6/site-packages/httplib2/__init__.py in request(self, uri, method, body, headers, redirections, connection_type)
   1924                         headers,
   1925                         redirections,
-> 1926                         cachekey,
   1927                     )
   1928         except Exception as e:

/opt/conda/lib/python3.6/site-packages/httplib2/__init__.py in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
   1593 
   1594         (response, content) = self._conn_request(
-> 1595             conn, request_uri, method, body, headers
   1596         )
   1597 

/opt/conda/lib/python3.6/site-packages/httplib2/__init__.py in _conn_request(self, conn, request_uri, method, body, headers)
   1506             except socket.gaierror:
   1507                 conn.close()
-> 1508                 raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
   1509             except socket.error as e:
   1510                 errno_ = (

ServerNotFoundError: Unable to find the server at www.googleapis.com

【问题讨论】:

  • gaierror: [Errno -3] Temporary failure in name resolution 检查您的互联网连接、DNS 设置、防火墙和路由设置。
  • 我在 kaggle 笔记本上做这个。我认为 DNS 和防火墙不会有问题,而且互联网工作正常。

标签: python


【解决方案1】:

至于正常的步骤,你应该先抓取一个页面,然后找到该页面的项目。

在您的代码中,步骤不清楚。您设置了 num=10,但您没有调用它,也设置了起始页。

# for example, get one page
res = service.cse().list(q=keyword, cx=cse_id).execute()


# find the next page
next_res = service.cse().list(q=query_keywords, cx=cse_id, num=10, start=res['queries']['nextPage'][0]['startIndex'],).execute()

希望它能解决我们的问题

【讨论】:

    猜你喜欢
    • 2017-03-29
    • 1970-01-01
    • 2020-11-26
    • 2016-04-28
    • 2011-05-29
    • 2020-09-24
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多