简体   繁体   中英

Flask & Tweepy - I can't handle the errors

I want to print when there is a problem with Tweepy. When I look at Tweepy's docs, it seems possible with the 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 .

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. Here, it gets defined in the scope of the try/except block, and so isn't accessible from outside of it, hence the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM