我的代码(相关部分)如下
myactivities = []
from urllib.error import HTTPError
for activity in client.get_activities(after = activities_from, before=activities_to):
print(activity)
print(activity.id, activity.start_date)
try:
temp = client.get_activity_streams(activity.id, types=types)
except urllib.error.HTTPError:
print("HTTPError")
myactivities.append(temp)
我得到的错误打印是这样的:
HTTPError Traceback (most recent call last)
<ipython-input-29-fc30cef1e4f3> in <module>()
8 try:
----> 9 temp = client.get_activity_streams(activity.id, types=types)
10 except urllib.error.HTTPError:
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in get_activity_streams(self, activity_id, types, resolution, series_type)
1174 # Pack streams into dictionary
-> 1175 return {i.type: i for i in streams}
1176
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in <dictcomp>(.0)
1174 # Pack streams into dictionary
-> 1175 return {i.type: i for i in streams}
1176
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in __next__(self)
1529 def __next__(self):
-> 1530 return self.next()
1531
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in next(self)
1535 if not self._buffer:
-> 1536 self._fill_buffer()
1537 try:
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\client.py in _fill_buffer(self)
1506
-> 1507 raw_results = self.result_fetcher(page=self._page, per_page=self.per_page)
1508
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\protocol.py in get(self, url, check_for_errors, use_webhook_server, **kwargs)
245 params = dict([(k, v) for k, v in kwargs.items() if not k in referenced])
--> 246 return self._request(url, params=params, check_for_errors=check_for_errors, use_webhook_server=use_webhook_server)
247
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\protocol.py in _request(self, url, params, files, method, check_for_errors, use_webhook_server)
169 if check_for_errors:
--> 170 self._handle_protocol_error(raw)
171
C:\Users\Koti\Anaconda3\lib\site-packages\stravalib\protocol.py in _handle_protocol_error(self, response)
214 if x is not None:
--> 215 raise x
216
HTTPError: 404 Client Error: Not Found [Record Not Found: []]
During handling of the above exception, another exception occurred:
NameError Traceback (most recent call last)
<ipython-input-29-fc30cef1e4f3> in <module>()
8 try:
9 temp = client.get_activity_streams(activity.id, types=types)
---> 10 except urllib.error.HTTPError:
11 print("HTTPError")
12 myactivities.append(temp)
NameError: name 'urllib' is not defined
如何成功捕获该HTTPError?我尝试了catch specific HTTP error in python中的所有建议,但没有帮助。
最佳答案
尝试
myactivities = []
from urllib.error import HTTPError
for activity in client.get_activities(after = activities_from, before=activities_to):
print(activity)
print(activity.id, activity.start_date)
try:
temp = client.get_activity_streams(activity.id, types=types)
except HTTPError:
print("HTTPError")
myactivities.append(temp)
您已经导入了
HTTPError
,必须直接使用。您的代码显示错误。
NameError Traceback (most recent call last)
<ipython-input-29-fc30cef1e4f3> in <module>()
8 try:
9 temp = client.get_activity_streams(activity.id, types=types)
---> 10 except urllib.error.HTTPError:
11 print("HTTPError")
12 myactivities.append(temp)
NameError: name 'urllib' is not defined
由于无法访问
urllib
,因此会出现错误。您从中导入了HTTPError
。