[英]Flask & Tweepy - I can't handle the errors
I want to print when there is a problem with Tweepy.当 Tweepy 出现问题时,我想打印。 When I look at Tweepy's docs, it seems possible with the try except
.当我查看 Tweepy 的文档时, try except
似乎是可能的。 But after try except, user variable does not come.但是尝试除之后,用户变量不来。 I get this error in user_profile_image: UnboundLocalError: local variable 'user' referenced before assignment
.我在 user_profile_image: UnboundLocalError: local variable 'user' referenced before assignment
中收到此错误。
How can I solve this problem?我怎么解决这个问题? Thank you谢谢
@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')
You need to define user
with a statement like user = None
before the try/except block.您需要在 try/except 块之前使用user = None
之类的语句定义user
。 Here, it gets defined in the scope of the try/except block, and so isn't accessible from outside of it, hence the exception.在这里,它在 try/except 块的 scope 中定义,因此无法从其外部访问,因此是异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.