2016-12-05 26 views
0

Hey私のコードは現在、ライブのつぶやきをデータベースにストリーミングしています。しかし、コードはしばらくの間、5〜10分実行されます。それは最終的に私に次のエラーを与えるし、終了します:Tweepy JSON: 'NoneType'オブジェクトに属性 '__getitem__'がありません

ファイル "twittergeo.py"、ライン198を、

ファイル "/Library/Python/2.7/site-packages/tweepy/streaming.py" で、ファイル内の/ライブラリ/Python/2.7/site-packages/tweepy/streaming.py "、ライン361、_start内 self._run() ファイル" /ライブラリ/Python/2.7/site-packages/tweepy/streaming.py " _run 昇給例外ではPython/2.7 /サイトパッケージ/ tweepy/streaming.py」、行294、 TypeError例外: 'NoneType' オブジェクトには属性がありません 'GetItem関数を'

from tweepy import Stream 
from tweepy import OAuthHandler 
from tweepy.streaming import StreamListener 
import json 
import MySQLdb 

canada =[-141.0,41.7,-51.0,83.7] 

consumer_key = '????????????' 
consumer_secret = '??????????????' 
access_token = '????????????????????????' 
access_secret = '??????????????' 

class TweetListener(StreamListener): 




    def on_data(self, data): 
     alldata = json.loads(data) 
     newdata = json.dumps(data) 

     created_at =  alldata["created_at"] #primary key 
     tweetId =   alldata["id"] 
     text =    alldata["text"]#must be above the dictlists 

     userId =   alldata["user"]["id"] #primarykey 
     twitterHandle =  alldata["user"]["screen_name"] 
     name =    alldata["user"]["name"] 
     location =   alldata["user"]["location"] 
     url =    alldata["user"]["url"] 
     bio =    alldata["user"]["description"] 
     protected =   alldata["user"]["protected"] 
     followers_count = alldata["user"]["followers_count"] 
     friends_count =  alldata["user"]["friends_count"] 
     geo_enabeled =  alldata["user"]["geo_enabled"] 
     lang =    alldata["user"]["lang"] 
     profile_image_url = alldata["user"]["profile_image_url"] 

     placeId =   alldata["place"]["id"]#primarykey 
     cityName =   alldata["place"]["name"] 
     fullName =   alldata["place"]["full_name"] 
     country_code =  alldata["place"]["country_code"] 
     country =   alldata["place"]["country"] 
     bounding_box =  alldata["place"]["bounding_box"] #bug 

     hashtags =   alldata["entities"]["hashtags"] #bug 
     user_mentions =  alldata["entities"]["user_mentions"] 

    return True 

    def on_error(self, status): 

auth = OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_secret) 

stream = Stream(auth, TweetListener()) 
stream.filter(locations=canada) 

IvはStackOverflowを見て、解決策を試しました。どれもうまくいかない。

答えて

2

まあ、私は198が

bounding_box =  alldata["place"]["bounding_box"] #bug 

または辞書alldataから「アイテムを取得」しようとしている任意の行でその行を前提とするつもりです。

エラーTypeError: 'NoneType' object has no attribute 'getitem'は、NoneTypeオブジェクトからオブジェクトにアクセスしようとしていることを示します。数分後にコードがクラッシュする理由は、おそらく多くのリクエストの1つが空または部分的に空の辞書を返すためです。

あなたがこれをやろうとしているかのようにです...

alldata = None 
bounding_box = alldata["place"]["whatever"] 

これを解決するために、私はそう

try: 
    res = on_data(data) 
except Exception as e: 
    print(e) # Just for debuggin purposes 
+0

おかげのようなon_data周りに巨大なtry-catchブロックを置きます。エラーは私のプログラムがbounding_box変数をどのように扱っていたかに起因していました。 JSONデータに変数bounding_boxがないことがあります。 –

関連する問題