Python 烧瓶及;Tweepy-我能';我不能处理这些错误
我想在Tweepy出现问题时打印。当我查看Tweepy的文档时,似乎可以使用Python 烧瓶及;Tweepy-我能';我不能处理这些错误,python,tweepy,Python,Tweepy,我想在Tweepy出现问题时打印。当我查看Tweepy的文档时,似乎可以使用try-except。但在try-except之后,用户变量没有出现。我在user\u profile\u image中得到此错误:UnboundLocalError:分配前引用的局部变量“user” 我怎样才能解决这个问题?多谢各位 @app.route("/twitter", methods=['GET', 'POST']) def twitter(): if request.method
try-except
。但在try-except之后,用户变量没有出现。我在user\u profile\u image中得到此错误:UnboundLocalError:分配前引用的局部变量“user”
我怎样才能解决这个问题?多谢各位
@app.route("/twitter", methods=['GET', 'POST'])
def twitter():
if request.method == 'POST':
username = request.form.get('text')
auth = tweepy.OAuthHandler("myoauthhandlers")
auth.set_access_token("myaccesstokens")
api = tweepy.API(auth)
try:
user = api.get_user(username)
except tweepy.TweepError as e:
print("Error")
user_profile_image = user.profile_image_url.replace('normal', '400x400')
在try/except块之前,需要使用类似于
user=None
的语句定义user
。在这里,它是在try/except块的范围内定义的,因此无法从它的外部访问,因此出现异常。它说AttributeError:'NoneType'对象没有属性'profile\u image\u url'
@zicxor然后您捕获了一个tweepy.tweeperor
异常,因此,user
变量没有在块中初始化。除了打印错误,您还应该退出当前函数,或者运行与profile\u image\u url
line不同的代码。首先,在异常处理之前,您应该为用户定义一个默认值。但您需要找出引发异常的原因。