如何从Twitter Rest API存储json对象

时间:2017-11-12 15:42:26

标签: python json mongodb rest twitter

嘿,我编写了一个python脚本,将Twitter Rest API中的一些数据存储到我的mongodb中。 它适用于流API,但现在我得到了Rest API的错误。 有人能帮助我吗?

这是我的代码和错误:

代码:

 searchQuery = '#python'  # this is what we're searching for
    maxTweets = 10000000 # Some arbitrary large number
    tweetsPerQry = 100  # this is the max the API permits

    sinceId = None
    max_id = -1L

client = MongoClient(MONGO_HOST)
db = client.alphaQuestDB

tweetCount = 0
print("Downloading max {0} tweets".format(maxTweets))
while tweetCount < maxTweets:
    try:
        if (max_id <= 0):
            if (not sinceId):
                new_tweets = api.search(q=searchQuery, count=tweetsPerQry)
            else:
                new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                        since_id=sinceId)
        else:
            if (not sinceId):
                new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                        max_id=str(max_id - 1))
            else:
                new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
                                        max_id=str(max_id - 1),
                                        since_id=sinceId)
        if not new_tweets:
            print("No more tweets found")
            break
        #datajson = json.loads(new_tweets)
        #created_at = datajson['created_at']
        for tweet in new_tweets:
            print(tweet)
            print(type(tweet))
            datajson = json.loads(tweet)
            db.alphaCollection.insert(datajson)
   tweetCount += len(new_tweets)
        print("Downloaded {0} tweets".format(tweetCount))
        max_id = new_tweets[-1].id
    except tweepy.TweepError as e:
        # Just exit if any error
        print("some error : " + str(e))
        break



print("Downloaded {0} tweets, Saved to {1}".format(tweetCount, 'MongoDB'))

这是错误:

<class 'tweepy.models.Status'>
Traceback (most recent call last):
  File "/home/krodi/eclipse-workspace/alphaQuestTechTrends/Twitter_REST_API.py", line 59, in <module>
    datajson = json.loads(tweet)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

1 个答案:

答案 0 :(得分:0)

您要跟踪的关键字中是否有空格?如果是这样,建议尝试删除任何带空格的关键字,并将关键字放在单引号中。这是一个封闭的问题与tweepy回购(见#748):https://github.com/tweepy/tweepy/issues/748