按位置过滤推文

Filtering Tweets By Location

我正在尝试修改此脚本以仅保存具有附加位置的推文的 JSON,并且 运行 遇到了 Python 的问题,其中检查某些内容不是null 似乎不起作用。 Has Key 无法正常工作,因为它们都有密钥,其中大多数只是 'null'。 Is not None 不起作用,因为 Python 认为 null 和 None 不同,并且将其检查为不是 "null" 的文本也不起作用。有没有人知道如何解决这个问题?

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


#Variables that contains the user credentials to access Twitter API 
access_key = '' #redacted for privacy and such
access_secret = ''
consumer_key = ''
consumer_secret = ''

#Runs auth to Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)


#This is a basic listener that will print incoming data to stdout
class StdOutListener(StreamListener):

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status


#Customizes the stream and saves text and lang to databases 
class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, api):
        self.api = api
        super(tweepy.StreamListener, self).__init__()
        self.db = pymongo.MongoClient('localhost', 27017).crime



    def on_data(self, data):
        jd = json.loads(data)
        if jd.has_key('coordinates') :
            self.db.tweets.insert( { 'text' : jd['text'], 'coordinates' : jd['coordinates'], 'lang' : jd['lang'] } )




    def on_error(self, status_code):
        return True # Don't kill the stream

    def on_timeout(self):
        return True # Don't kill the stream

#Calls on StreamListerner and provides specifications of tracking
l = tweepy.streaming.Stream(auth, CustomStreamListener(api))
l.filter(track=['guns'])

您可以尝试检查字符串的长度:

if len( jd['coordinates'] ) > 1:
    self.db.tweets.insert( { 'text' : jd['text'], 'coordinates' : jd['coordinates'], 'lang' : jd['lang'] } )