[英]How to store json Objects from Twitter Rest API
嘿,我寫了一個Python腳本來將Twitter Rest API中的一些數據存儲到我的mongodb中。 它適用於流式API,但現在我遇到了其他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
您要跟蹤的關鍵字中是否有空格? 如果是這樣,建議嘗試刪除所有帶有空格的關鍵字,並將關鍵字放在單引號中。 這是tweepy回購協議的已解決問題(請參閱#748): https : //github.com/tweepy/tweepy/issues/748
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.