Python Tweepy JSON:';非类型';对象没有属性'__获取项目';
嘿,我的代码目前正在将实时推特流到数据库。但是,代码将运行一段时间,5-10分钟;它最终会给我以下错误并退出: 文件“twittergeo.py”,第198行,在 文件“/Library/Python/2.7/site packages/tweepy/streaming.py”,第445行,在过滤器中 自启动(异步) 文件“/Library/Python/2.7/site-packages/tweepy/streaming.py”,第361行,开头 self._run() 文件“/Library/Python/2.7/site packages/tweepy/streaming.py”,第294行,正在运行 引发异常 TypeError:“非类型”对象没有属性“getitem”Python Tweepy JSON:';非类型';对象没有属性'__获取项目';,python,json,python-2.7,tweepy,Python,Json,Python 2.7,Tweepy,嘿,我的代码目前正在将实时推特流到数据库。但是,代码将运行一段时间,5-10分钟;它最终会给我以下错误并退出: 文件“twittergeo.py”,第198行,在 文件“/Library/Python/2.7/site packages/tweepy/streaming.py”,第445行,在过滤器中 自启动(异步) 文件“/Library/Python/2.7/site-packages/tweepy/streaming.py”,第361行,开头 self._run() 文件“/Library
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并尝试了一些解决方案;似乎没有一个可行。好吧,我假设198行是
bounding_box = alldata["place"]["bounding_box"] #bug
或者任何试图从字典中“获取项目”的行alldata
错误TypeError:“NoneType”对象没有属性“getitem”
表示您正试图从NoneType
对象访问对象。代码在几分钟后崩溃的原因可能是因为您发出的许多请求中有一个返回了一个空的或部分空的字典
就好像你正试图这么做
alldata = None
bounding_box = alldata["place"]["whatever"]
为了解决这个问题,我会在_data上的周围放置一个巨大的try-catch块,就像这样
try:
res = on_data(data)
except Exception as e:
print(e) # Just for debuggin purposes
我假设198行是
bounding_box = alldata["place"]["bounding_box"] #bug
或者任何试图从字典中“获取项目”的行alldata
错误TypeError:“NoneType”对象没有属性“getitem”
表示您正试图从NoneType
对象访问对象。代码在几分钟后崩溃的原因可能是因为您发出的许多请求中有一个返回了一个空的或部分空的字典
就好像你正试图这么做
alldata = None
bounding_box = alldata["place"]["whatever"]
为了解决这个问题,我会在_data上的周围放置一个巨大的try-catch块,就像这样
try:
res = on_data(data)
except Exception as e:
print(e) # Just for debuggin purposes
谢谢该错误是由于我的程序处理边界框变量的方式造成的。有时候JSON数据没有变量边界框..谢谢。该错误是由于我的程序处理边界框变量的方式造成的。有时JSON数据没有变量边界框。。