Parsing Twitter JSON object in Python

前端 未结 4 1257
一生所求
一生所求 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:07

    Instead of using global variables, I would reorganize the code in a python class:

    import tweepy
    
    class TweetPrinter():
        """
            Simple class to print tweets
        """
        def __init__(self, consumer_key, consumer_secret, access_token, 
                     access_token_secret):
            self.consumer_key = consumer_key
            self.consumer_secret = consumer_secret
            self.access_token = access_token
            self.access_token_secret = access_token_secret
            self.auth = tweepy.OAuthHandler(self.consumer_key, 
                                            self.consumer_secret)
            self.auth.set_access_token(access_token, access_token_secret)
    
        def tweet_print(self):
            api = tweepy.API(self.auth)
            football_tweets = api.search(q="football")
            for tweet in football_tweets:
                print(tweet.text)
    
    
    def main():
        tweet_printer = TweetPrinter(my_consumer_key, my_consumer_secret, 
                                  my_access_token, my_access_token_secret)
    
        tweet_printer.tweet_print()
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
  • 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())
    
    0 讨论(0)
  • 2021-01-07 05:20

    take my code for tweepy:

    def twitterfeed():
       auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
       auth.set_access_token(access_key, access_secret)
       api = tweepy.API(auth)
       statuses = tweepy.Cursor(api.home_timeline).items(20)
       data = [s.text.encode('utf8') for s in statuses]
       print data
    
    0 讨论(0)
  • 2021-01-07 05:23

    Tweepy gives you richer objects; it parsed the JSON for you.

    The SearchResult objects have the same attributes as the JSON structures that Twitter sent; just look up the Tweet documentation to see what is available:

    for result in api.search(q="football"):
        print result.text
    

    Demo:

    >>> import tweepy
    >>> tweepy.__version__
    '3.3.0'
    >>> consumer_key = '<consumer_key>'
    >>> consumer_secret = '<consumer_secret>'
    >>> access_token = '<access_token>'
    >>> access_token_secret = '<access_token_secret>'
    >>> auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    >>> auth.set_access_token(access_token, access_token_secret)
    >>> api = tweepy.API(auth)
    >>> for result in api.search(q="football"):
    ...     print result.text
    ... 
    Great moments from the Women's FA Cup http://t.co/Y4C0LFJed9
    RT @freebets: 6 YEARS AGO TODAY: 
    
    Football lost one of its great managers. 
    
    RIP Sir Bobby Robson. http://t.co/NCo90ZIUPY
    RT @Oddschanger: COMPETITION CLOSES TODAY!
    
    Win a Premier League or Football League shirt of YOUR choice! 
    
    RETWEET &amp; FOLLOW to enter. http…
    Berita Transfer: Transfer rumours and paper review – Friday, July 31 http://t.co/qRrDIEP2zh [TS] #nobar #gosip
    @ajperry18 im sorry I don't know this football shit                                                                    
    0 讨论(0)
提交回复
热议问题