2

I am learning the tweepy library to access the twitter api. I have a csv file with some preliminary data (such as tweet_id) and I pulled that into a dataframe. I need to use that data to pull more data using tweepy.

I am trying to write that data to a text file and then create a new dataframe off of that. I have been trying different things the past couple evenings, and I don't understand why this isn't writing the data to the text file. I have all the necessary tokens stored in variables.

auth = tweepy.OAuthHandler(Consumer_Key, Consumer_Secret)
auth.set_access_token(Access_Token, Access_Secret)
tweetapi = tweepy.API(auth, 
wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
#writing text file
txtfile = open("jsontweet3.txt", "a")
txtfile.write('tweet_id retweet_count favorite_count''\n')
#pulling tweet info
for tweet_id in fdf.tweet_id:
  try:
    twitinfo = tweetapi.get_status(str(tweet_id),tweet_mode='extended')
    retweets = twitinfo.retweet_count
    favorites = twitinfo.favorite_count
    txtfile.write(twitinfo+' '+str(retweets)+' '+str(favorites)+'\n')

txtfile.close()

I would be greatly appreciative of any help!

Jw007
  • 59
  • 7
  • What is happening? Simply no data being written? Also, why do you have a `try` clause with no `except`? that should raise a SyntaxError – WillMonge Apr 21 '18 at 01:54

1 Answers1

1

I am not clear on what the error is, and it may just be because of the try clause.

Here are a few pieces of advice that I hope may come handy:

  • try clause:

    • Needs an except clause, if not will raise SyntaxError. If you don't want anything then except: pass , but you should really not use that: Why is except pass a bad programming practice
    • Always try to limit the code inside the try to the minimum possible, ideally only the line of code that can fail
  • Read/write to file:

    • Usually the best practice is to use with (called a context manager), which basically does the open and close for you, but in a safer way, because if anything inside the with goes wrong it will still close the file. See example below:
with open('file.txt', 'a') as f:
    f.write('foobar')

Using these a possible re-write of your code would look like:

auth = tweepy.OAuthHandler(Consumer_Key, Consumer_Secret)
auth.set_access_token(Access_Token, Access_Secret)
tweetapi = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)


failed_tweets = []  # keep track of the tweets that fail

#writing text file
with open("jsontweet3.txt", "a") as txtfile:
    txtfile.write('tweet_id retweet_count favorite_count \n')

    #pulling tweet info
    for tweet_id in fdf.tweet_id:
        try:
            twitinfo = tweetapi.get_status(str(tweet_id), tweet_mode='extended')

        except:
            # Not able to get tweet --> add to failed_tweets list
            failed_tweets.append(tweet_id)

        else:
            # only gets executed if the try clause did not fail         
            retweets = twitinfo.retweet_count
            favorites = twitinfo.favorite_count
            txtfile.write(str(twitinfo)+' '+str(retweets)+' '+str(favorites)+'\n')
WillMonge
  • 1,005
  • 8
  • 19
  • Hi, sorry if I was not clear, but my issue is that the data from twitter is not being written to the text file. Thanks for your help! this code is throwing the following error: "unsupported operand type(s) for +: 'Status' and 'str'" – Jw007 Apr 21 '18 at 21:14
  • then it seems we found why your code was not writing to the text file. The problem is with the last line. Convert the `twitinfo` to a string and let me know. I have edited the code to do a simple `str()` but maybe there is better way to do it.. like a .text or something, based on the information that you want. – WillMonge Apr 21 '18 at 23:14
  • I made some minor changes to the your code, and am now writing to the file! thank you! – Jw007 Apr 21 '18 at 23:16
  • great! glad to hear :) – WillMonge Apr 21 '18 at 23:16
  • Any chance you would what is happening here? https://stackoverflow.com/questions/49961156/transforming-tweepy-data-from-text-file-into-dataframe – Jw007 Apr 21 '18 at 23:45