简体   繁体   English

Twitter流:尝试/除外IndexError

[英]Twitter Streaming: Try/Except for IndexError

Background: I want to download specific parts of users tweets (eg username, id, expanded url, etc) from the twitter api. 背景:我想从twitter api下载用户tweet的特定部分(例如,用户名,id,扩展的url等)。 I am able to do so successfully. 我能够成功地做到这一点。

Problem: Because not all users have expanded_url , I sometimes receive the following error: 问题:因为并非所有用户都拥有expanded_url ,所以有时我会收到以下错误消息:

IndexError: list index out of range

Goal: If such IndexError occur, skip and proceed to collect new tweets 目标:如果发生此类IndexError ,请跳过并继续收集新的tweet

I think one way to solve this problem is by using a try/except statement 我认为解决此问题的一种方法是使用try / except语句

Questions: Is a try/except statement a valid way to do achieve this goal? 问题: try / except语句是否是实现此目标的有效方法? If so, how do I properly apply a try/except statement? 如果是这样,如何正确应用try / except语句?

I have tried the following: 我尝试了以下方法:

class StdOutListener(StreamListener):
         def on_data(self, data):
             t = json.loads(data)
             tweet_id = t['id_str'] 
             user_name = t['user']['name'] 
             try:
                 expanded_url = t['entities']['urls'][0]['expanded_url']
             except:
                 pass 

But I get the following error: 但是我收到以下错误:

UnboundLocalError: local variable 'expanded_url' referenced before assignment

I have searched around SO, and have a few examples similar to my question ( UnboundLocalError: local variable 'url_request' referenced before assignment , UnboundLocalError: local variable 'url' referenced before assignment ) 我在SO周围进行了搜索,并找到了一些与我的问题类似的示例( UnboundLocalError:分配前引用了本地变量'url_request'UnboundLocalError: 分配前引用了 本地变量'url'

But I'm not sure how to directly change my code so that I can implement the try/except statement. 但是我不确定如何直接更改代码,以便实现try / except语句。 I am also open to other ways to solve this problem. 我也愿意以其他方式解决这个问题。 Thank you! 谢谢!

When the block inside the try has an error, the block inside the except gets executed. 当try内的块有错误时,except内的块将被执行。 If nothing happens in your except (it only passes) expanded_url is never defined. 如果您的例外(仅通过)中什么也没有发生,则永远不会定义Expanded_url。 You should assign expanded_url inside the except block to a default value or empty strings, whatever it should be: 您应该将except块内的expanded_url分配给默认值或空字符串,无论它是什么:

         try:
             expanded_url = t['entities']['urls'][0]['expanded_url']
         except:
             expanded_url = ''

The problem is that you are probably trying to use the expanded_url after the try/except block but when your code throws the exception you don't create the variable. 问题是您可能试图在try/except块之后使用expanded_url ,但是当您的代码引发异常时,您不会创建变量。 Try to either: 尝试之一:

expanded_url = ''
try:
    expanded_url = ....
except:
    pass

Or: 要么:

try:
    expanded_url = ....
except:
    expanded_url = ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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