Search code examples
pythonpython-2.7twittertweepy

extract tweets with some special keywords from twitter using tweepy in python


here is my code..i want to extract tweets from twitter with some keywords....my code dont give any errors but i am not getting the output file generated...please help me........

import re
import csv
import tweepy
from tweepy import OAuthHandler
#TextBlob perform simple natural language processing tasks.
from textblob import TextBlob



def search():
    #text = e.get() **************************

consumer_key = ''
consumer_secret = ''
access_token = ' '
access_token_secret = ' '
# create OAuthHandler object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
auth.set_access_token(access_token, access_token_secret)
# create tweepy API object to fetch tweets
api = tweepy.API(auth)

def get_tweets(query, count = 300):

   # empty list to store parsed tweets
   tweets = []
   target = open("tweets.txt", 'w',encoding="utf-8")
   t1 = open("review.txt", 'w',encoding="utf-8")
   # call twitter api to fetch tweets
   q=str(query)
   a=str(q+" sarcasm")
   b=str(q+" sarcastic")
   c=str(q+" irony")
   fetched_tweets = api.search(a, count = count)+ api.search(b, count = count)+ api.search(c, count = count)
   # parsing tweets one by one
   print(len(fetched_tweets))

   for tweet in fetched_tweets:

       # empty dictionary to store required params of a tweet
       parsed_tweet = {}
       # saving text of tweet
       parsed_tweet['text'] = tweet.text
       if "http" not in tweet.text:
           line = re.sub("[^A-Za-z]", " ", tweet.text)
           target.write(line+"\n")
           t1.write(line+"\n")
   return tweets

   # creating object of TwitterClient Class
   # calling function to get tweets
tweets = get_tweets(query =text, count = 20000)

root.mainloop()

From this code i am nor getting the output generated file. Can anyone tell me what i am doing wrong ? Thanks in advance!


Solution

  • I just made some slight changes and it was working perfectly for me. Removed or commented some unnecessary statements (like the review file). Changed the open function to io.open since I have python version 2.7. Here is the running code, hope it helps!!

    `

    import re
    import io
    import csv
    import tweepy
    from tweepy import OAuthHandler
    #TextBlob perform simple natural language processing tasks.
    #from textblob import TextBlob
    
    
    consumer_key = 'sz6x0nvL0ls9wacR64MZu23z4'
    consumer_secret = 'ofeGnzduikcHX6iaQMqBCIJ666m6nXAQACIAXMJaFhmC6rjRmT'
    access_token = '854004678127910913-PUPfQYxIjpBWjXOgE25kys8kmDJdY0G'
    access_token_secret = 'BC2TxbhKXkdkZ91DXofF7GX8p2JNfbpHqhshW1bwQkgxN'
    # create OAuthHandler object
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    # set access token and secret
    auth.set_access_token(access_token, access_token_secret)
    # create tweepy API object to fetch tweets
    api = tweepy.API(auth)
    
    
    
    
    def get_tweets(query, count = 300):
    
        # empty list to store parsed tweets
        tweets = []
        target = io.open("mytweets.txt", 'w', encoding='utf-8')
        # call twitter api to fetch tweets
        q=str(query)
        a=str(q+" sarcasm")
        b=str(q+" sarcastic")
        c=str(q+" irony")
        fetched_tweets = api.search(a, count = count)+ api.search(b, count = count)+ api.search(c, count = count)
        # parsing tweets one by one
        print(len(fetched_tweets))
    
        for tweet in fetched_tweets:
    
            # empty dictionary to store required params of a tweet
            parsed_tweet = {}
            # saving text of tweet
            parsed_tweet['text'] = tweet.text
            if "http" not in tweet.text:
                line = re.sub("[^A-Za-z]", " ", tweet.text)
                target.write(line+"\n")
        return tweets
    
        # creating object of TwitterClient Class
        # calling function to get tweets
    tweets = get_tweets(query ="", count = 20000)
    

    `