Reputation: 77
I am using the tweepy API to stream specific information and then store that into a CSV. It seems tweets are storing successfully but I keep getting the message ('failed ondata', 'coercing to Unicode: need string or buffer, NoneType found')
What does this message mean and how can I go about fixing it?
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import time
import json, csv
import sys
import twitter_credentials
class listener(StreamListener):
def on_data(self, data):
try:
tweet = json.loads(data)
text = tweet["text"]
username = tweet["user"]["screen_name"]
location = tweet["user"]["location"]
created_at = tweet["created_at"]
saveTweets = open('SaveTweets.csv', 'a') #open a csv which we will append to
reload(sys)
sys.setdefaultencoding('utf-8')
saveTweets.write( "created_at : " + created_at +" Username : " + username + "Tweet Text : " + text + "Location : " + location)
saveTweets.write('\n')
saveTweets.close()
return True
except BaseException, e:
print('failed ondata', str(e))
time.sleep(5)
def on_error(self, status):
print(status)
auth = OAuthHandler(twitter_credentials.API_KEY, twitter_credentials.API_SECRET_KEY)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["plumber"])
Upvotes: 0
Views: 102
Reputation: 2015
See if this helps https://stackoverflow.com/a/10958477/9592801 and also you are trying to write NoneType
I guess.
EDIT:
Lets assume your tweet json might look something like this and if you try to concatenate unicode with None
you will encounter exactly same TypeError
.
tweet = {
"text": "some text",
"user": {
"screen_name": "screen 1",
"location": "location 1"
},
"created_at": None
}
text = tweet["text"]
username = tweet["user"]["screen_name"]
location = tweet["user"]["location"]
created_at = tweet["created_at"]
data_to_write = u"created_at : " + created_at + u" Username : " + str(username) + u"Tweet Text : " + str(text) + u"Location : " + str(location)
output:
...
data_to_write = u"created_at : " + created_at + u" Username : " + str(username) + u"Tweet Text : " + str(text) + u"Location : " + str(location)
TypeError: coercing to Unicode: need string or buffer, NoneType found
Now if you wrap with str()
you will come over this error as it will convert None
to string.
data_to_write = u"created_at : " + str(created_at) + u" Username : " + str(username) + u"Tweet Text : " + str(text) + u"Location : " + str(location)
If this didnt solve your issue then please share some more details about your error(line number, python version etc) deep dive can be done. For example getting more info from exception.
import traceback;
import sys
print(sys.version)
try:
...
except BaseException as e:
traceback.print_exc(err)
Upvotes: 1