1

到目前为止,我有以下代码可以工作并将推文插入到我的 mongodb 中,但我有几个问题。

class CustomStreamListener(tweepy.StreamListener):
def __init__(self, api):
    self.api = api
    super(tweepy.StreamListener, self).__init__()

    self.db = pymongo.MongoClient().test

def on_data(self, tweet):
    self.db.tweets.insert(json.loads(tweet))

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

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


sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
sapi.filter(track=['arsenal'] , languages = ['en'])
  1. 有人可以解释一下我如何只能将推文的某些部分插入数据库,即。只是推文文本和位置。

  2. twitter 流 api 是否允许仅显示推文而不显示@回复推文?

4

1 回答 1

1

json.loads(tweet)只是一个dictionary,你可以自由选择你处理它的键值的哪些部分。

您可以通过以任何您喜欢的方式调节推文来过滤推文:

tweet_obj = json.loads(tweet)
if not tweet_obj['in_reply_to_user_id']: # replies has `None` in this field
    pass                                 # add some processing here
于 2013-11-05T16:25:38.823 回答