Parsing Twitter JSON object in Python

前端 未结 4 1259
一生所求
一生所求 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()
    

提交回复
热议问题