简体   繁体   English

如何从Twitter Rest API存储JSON对象

[英]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.

相关问题 Python中来自Twitter API的无效JSON对象 - Invalid JSON objects from Twitter API in Python 如何从 REST API 下载 JSON 数据集 - How to download JSON dataset from REST API 如何从Twitter API使用JSON获取完整的状态文本? - how to get the full status text from twitter API with JSON? 如何在Python中遍历JSON对象(Twitter API搜索的结果)并保存到CSV文件? - How to iterate through JSON objects (results of twitter api search) in Python and save to a CSV file? 如何使用python从REST API中读取对象? - How to read objects from REST API using python? 如何存储来自 Tweepy 模块 Twitter API v2 的 StreamingClient.filter() 或 .sample() 的数据 - How to store data from StreamingClient.filter() or .sample() from the Tweepy Module Twitter API v2 AWS Glue - 将来自 GET(REST API) 请求的 Json 响应转换为 DataFrame/DyanamicFramce 并将其存储在 s3 存储桶中 - AWS Glue - Convert the Json response from GET(REST API) request to DataFrame/DyanamicFramce and store it in s3 bucket 我怎么能从Twitter的流媒体API中消费推文并将它们存储在mongodb中 - How can I consume tweets from Twitter's streaming api and store them in mongodb 来自Twitter API的vcr.py商店调用 - vcr.py store call from twitter api JSON和Twitter API出现问题 - Trouble with JSON and the Twitter API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM