Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 烧瓶及;Tweepy-我能';我不能处理这些错误_Python_Tweepy - Fatal编程技术网

Python 烧瓶及;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

我想在Tweepy出现问题时打印。当我查看Tweepy的文档时,似乎可以使用
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不同的代码。首先,在异常处理之前,您应该为用户定义一个默认值。但您需要找出引发异常的原因。