python - Exception shows up in wrong place -
in t.cursor() method, exception twython library thrown small percentage of ids. however, whenever exception occurs, actual line in code it's thrown for-loop comes after try/except block, prevents continue being called.
how exception can thrown in try block, not caught except block, , show later during (mostly) unrelated code?
and yes, it's 401 error, that's twitter api returning wrong code. in reality i'm correctly authenticating. know can move except block after for-loop, want know how can happen @ all.
from twython import twython t = twython(...) # ... id in ids: try: # exception caused following line followers = t.cursor(t.get_followers_ids, id=id) except: # block never run print("exception user " + str(id)) continue # line throws exception, inexplicably follower_id in followers: values.append((follower_id, id, scrape_datetime)) # ... the traceback:
traceback (most recent call last): file "/root/twitter/nightly.py", line 5, in <module> t.get_followers(t.current_tweeters) file "/root/twitter/tweets.py", line 81, in get_followers follower_id in followers: file "/usr/local/lib/python3.3/dist-packages/twython-3.0.0-py3.3.egg/twython/api.py", line 398, in cursor content = function(**params) file "/usr/local/lib/python3.3/dist-packages/twython-3.0.0-py3.3.egg/twython/endpoints.py", line 212, in get_followers_ids return self.get('followers/ids', params=params) file "/usr/local/lib/python3.3/dist-packages/twython-3.0.0-py3.3.egg/twython/api.py", line 231, in return self.request(endpoint, params=params, version=version) file "/usr/local/lib/python3.3/dist-packages/twython-3.0.0-py3.3.egg/twython/api.py", line 225, in request content = self._request(url, method=method, params=params, api_call=url) file "/usr/local/lib/python3.3/dist-packages/twython-3.0.0-py3.3.egg/twython/api.py", line 195, in _request retry_after=response.headers.get('retry-after')) twython.exceptions.twythonautherror: twitter api returned 401 (unauthorized), error occurred processing request.
looks t.cursor(...) returns generator doesn't execute until iterate through it. while might appear connection happens at:
followers = t.cursor(t.get_followers_ids, id=id) it doesn't until iterate through generator loop. sort of mentioned here
if have need defer processing until later still want catch exception, turn generator list. exhaust generator , save data later.
followers = t.cursor(t.get_followers_ids, id=id) followers = list(followers)
Comments
Post a Comment