当前位置: 代码迷 >> python >> 从Twitter收集坐标字段时,无法获取“ NoneType”对象
  详细解决方案

从Twitter收集坐标字段时,无法获取“ NoneType”对象

热度:47   发布时间:2023-07-14 08:57:38.0

好的,所以我有一个从Twitter收集数据的鳕鱼,但是当包含坐标字段时出现错误。 该代码运行了几次,但出现此错误。

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import string
import json
import re

ckey ='mykey'
csecret ='mykey'
atoken = 'mykey'
asecret = 'mykey'

class listener(StreamListener):

    def on_data(self, data):
        try:
            jsonData=json.loads(data)
            geo_lat=jsonData['geo']['coordinates'][0] // field that get error
            geo_long=jsonData['geo']['coordinates'][1] // field that get erro
            savethis=+str(geo_lat)+', '+str(geo_long)          
            print unicode(savethis).encode("utf-8")
            return True
        except BaseException, e:
             print 'Falied on data,',str(e)
             time.sleep(5)
        def on_error(self, status):
            print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
#twitterStream.filter(locations=[-78,15,-70,18 ])
twitterStream.filter(locations=[-74,40,-73,41])

错误 -

Traceback (most recent call last):
File "C:/Users/Kevonia/Dropbox/Major Project/Python Workspace/database&filerwithbugs.py", line 46, in on_data
geo_lat=jsonData['geo']['coordinates'][0]
TypeError: 'NoneType' object is not subscriptable

jsonDatajsonData['geo']jsonData['geo']['coordinates']都设置为null ,在Python中转换为None

>>> None['whatever']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
  相关解决方案