Parsing Twitter JSON object in Python

前端 未结 4 1280
一生所求
一生所求 2021-01-07 04:39

I am trying to download tweets from twitter.

I have used python and Tweepy for this. Though I am new to both Python and Twitter API.

My Python script is as f

4条回答
  •  长发绾君心
    2021-01-07 05:16

    You can use the JSON parser to achieve this, here is my code on App Engine that handles a JSONP response ready to be used in with a JQuery client:

    import webapp2
    import tweepy
    import json
    from tweepy.parsers import JSONParser
    
    class APISearchHandler(webapp2.RequestHandler):
        def get(self):
    
            CONSUMER_KEY = 'xxxx'
            CONSUMER_SECRET = 'xxxx'
            ACCESS_TOKEN_KEY = 'xxxx'
            ACCESS_TOKEN_SECRET = 'xxxx'
    
            auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
            auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
            api = tweepy.API(auth, parser=JSONParser())
    
            # Query String Parameters
            qs = self.request.get('q')
            max_id = self.request.get('max_id')
    
            # JSONP Callback
            callback = self.request.get('callback')
    
            max_tweets = 100
            search_results = api.search(q=qs, count=max_tweets, max_id=max_id)
            json_str = json.dumps( search_results )
    
            if callback:
                response = "%s(%s)" % (callback, json_str)
            else:
                response = json_str
    
            self.response.write( response )
    

    So the key point is

    api = tweepy.API(auth, parser=JSONParser())
    

提交回复
热议问题