Skip to content Skip to sidebar Skip to footer

Tweepy Search Api Writing To File Error

Noob python user: I've created file that extracts 10 tweets based on the api.search (not streaming api). I get a screen results, but cannot figure how to parse the output to save t

Solution 1:

tweet is not a string or a character buffer. It's an object. Replace your line with saveFile.write(tweet.text) and you'll be good to go.

saveFile = open('MarSec.csv', 'a')
for tweet in MarSec:
    print" "print tweet.created_at, tweet.text, tweet.lang
    saveFile.write("%s %s %s\n"%(tweet.created_at, tweet.lang, tweet.text))

saveFile.close()

Solution 2:

I just thought I'd put up another version for those who might want to save all the attributes of a tweepy.models.Status object, if you're not yet sure which attributes of each tweet you want to save to file.

import json

search_results = []
forstatusin tweepy.Cursor(api.search, q=search_text).items(5000):
    search_results.append(status._json)

with open('search_results.json', 'w') as f:
    json.dump(search_results, f)

The first block will store the search results into a list of dictionaries, and the second block will output all the tweets into a json file. Please beware, this might use up a lot of memory if the size of your search results is very big.

Solution 3:

This is Twitter's classic error code when something is wrong while sending a wrong image.

Try to find images you are trying to upload and check the format of the images.

The only thing I did was erase the images that MY media player of Windows can´t read and that's all! the script run perfectly.

Post a Comment for "Tweepy Search Api Writing To File Error"