[英]How to store json Objects from Twitter Rest API
Hey i wrote a python script to store some data from the Twitter Rest API to my mongodb. 嘿,我写了一个Python脚本来将Twitter Rest API中的一些数据存储到我的mongodb中。 It worked for the streaming API but now i got an Error for the Rest API.
它适用于流式API,但现在我遇到了其他API的错误。 Can someone help me?
有人能帮我吗?
Here is my code and the Error: 这是我的代码和错误:
Code: 码:
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'))
And here is the Error: 这是错误:
<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
Are there spaces in the keywords you are tracking? 您要跟踪的关键字中是否有空格? If so, suggest trying to remove any keywords with a space and place keywords in single quotes.
如果是这样,建议尝试删除所有带有空格的关键字,并将关键字放在单引号中。 This is a closed issue with the tweepy repo (see #748): https://github.com/tweepy/tweepy/issues/748
这是tweepy回购协议的已解决问题(请参阅#748): https : //github.com/tweepy/tweepy/issues/748
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.