【发布时间】:2017-02-03 05:01:02
【问题描述】:
我想在一个列表中搜索 url,总共有大约 74 个 url。我使用 try 和 except 来告诉 python 跳过不响应的站点(带有 http 错误代码 400 等)
from googleapiclient.discovery import build
service = build("customsearch", "v1", developerKey='keyhere')
for i in range(0,k-1):
try:
queries = search_sources[i]
res = service.cse().list(q= queries, cx='idhere',).execute()
except urllib2.HTTPError:
continue
但问题是我收到有关某些网站有错误的消息(这意味着除了部分不起作用):
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googleapiclient/http.py", line 838, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/customsearch/v1?q=site%3Ahttp%3A%2F%2Fbit.ly%2FgktvnmChina+protest&alt=json&cx=myid&key=mykey returned "Bad Request">
【问题讨论】:
-
尝试更通用的
except行。例如except Exception:. -
@SonofaBeach 刚刚发布了整个错误
-
它说 googleapiclient.errors.HttpError 所以在 except 中使用它
-
你应该只做
for queries in search_sources:而不是手动操作i作为索引。
标签: python google-api except