Skip to main content
 首页 » 编程设计

python之Twitter 应用程序中的 '__getitem__' 对象中没有属性 'set'

2025年05月04日17arxive

我正在构建一个简单的应用程序,它只为用户流式传输 Twitter 流,并不断出现错误

File "scratch.py", line 6, in <module> 
config['oauth_token'], 
TypeError: 'set' object has no attribute '__getitem__'</code> 

第 6 行。

源代码是:

from twitter import TwitterStream 
 
config = { --- add your 4 horsemen in this dictionary --- } 
ts = TwitterStream( 
                    auth=OAuth( 
                        config['oauth_token'], 
                        config['oauth_token_secret'], 
                        config['key'], 
                        config['secret'] 
                        ) 
                    ) 
openstream = ts.statuses.filter(track=words) 
for item in openstream: 
    print item['user']['screen_name'], datetime.strptime(item['created_at'],'%a %b %d %H:%M:%S +0000 %Y'), item['text'] 
    if datetime.now() &gt; stop: 
        print datetime.now().isoformat() 
        break 

“oauth-token”和其他变量的值当然是特定于应用程序的,我不能在这里透露它们。我是一个 python 新手,不能真正理解这里的错误,很高兴得到任何帮助。

请您参考如下方法:

您的配置字典中某处有一个拼写错误,导致它被创建为一个集合,而不是一个字典。也许您在某处键入了 , 而不是 :。