twitterscraper icon indicating copy to clipboard operation
twitterscraper copied to clipboard

AttributeError: Tweet instance has no attribute 'encode'

Open aneliram89 opened this issue 7 years ago • 9 comments

I'm simply running the code from the readme file and i keep getting errors. Can you help with this error?

And also, this: twitterscraper Trump -l 100 -o tweets.json Produces Error: problem

aneliram89 avatar Nov 07 '17 11:11 aneliram89

Which Python version are you running? I think this problem should not appear with Python 3

taspinar avatar Nov 09 '17 18:11 taspinar

Hey there! I have the same issue when I try to work directly from within Python. This is what happens

file = open(“output.txt”,”w”) for tweet in query_tweets("Trump OR Clinton", 10): file.write(tweet.encode('utf-8')) file.close()

AttributeError: 'Tweet' object has no attribute 'encode'

casbah92 avatar Nov 10 '17 14:11 casbah92

A little workaround could be :

data=query_tweets("Trump OR Clinton", 1000) with open('data.json', encoding='utf-8',mode='w') as outfile: json.dump(str(data), outfile)

But you'll have to remove the quotes marks manually from the file if you want to load it as a json object... I'm still a beginner so that's all I could come up with. Hope it's helpful.

afaf-taik avatar Dec 16 '17 05:12 afaf-taik

@casbah92 query_tweets returns a list of instances of the class Tweet. This class has as attributes among others user, fullname, text, etc. So encoding the entire class as utf-8 does not make much sense. If you are only interested in the texts of the tweets, you could save it as file.write(tweet.text.encode('utf-8')). Otherwise you can save the entire list with Tweet instances as a json, as suggested by @afaf-taik

taspinar avatar Jan 11 '18 08:01 taspinar

Hi All:

I've tried to do the same thing as afaf-taik and taspinar mentioned. Despite the code run successfully, the output is always json with a series of objects like <twitterscraper.tweet.Tweet object at 0x104dbf4a8>. Any idea? BTW, I've tried in both python2.7 and python3.6. It seems that encoding as 'utf-8' somehow is not working properly. When I used python2.7, I could run "file.write(tweet.text.encode('utf-8'))" successfully and obtain meaningful output.

Thanks! James

jyuan1986 avatar Jan 30 '18 00:01 jyuan1986

Hey there! I have the same issue when I try to work directly from within Python. This is what happens

file = open(“output.txt”,”w”) for tweet in query_tweets("Trump OR Clinton", 10): file.write(tweet.encode('utf-8')) file.close()

AttributeError: 'Tweet' object has no attribute 'encode'

try file.write(str(tweet.text.encode('utf-8')))

ghost avatar Sep 19 '18 11:09 ghost

try file.write(str(tweet.text.encode('utf-8')))

I tried but output.txt was full of unreadable byte code.

student-101 avatar Apr 03 '19 05:04 student-101

try file.write(str(tweet.text.encode('utf-8')))

I tried but output.txt was full of unreadable byte code.

I found it was useful in python3.6

lv2020 avatar Apr 09 '19 13:04 lv2020

For those who arrive via Google, I recommend this:

from twitterscraper import query_tweets_from_user
import json

username = "astrorobotic"
filename = "{}.json".format(username)

tweets = query_tweets_from_user(username)
print("Found: {} tweets".format(len(tweets)))

j = []
for t in tweets:
    t.timestamp = t.timestamp.isoformat()
    print("{} {} {}: {}".format(t.is_retweet, t.tweet_id, t.timestamp, t.text))
    j.append(t.__dict__)

with open(filename, "w") as f:
    f.write(json.dumps(j))

davidbernat avatar Nov 02 '19 20:11 davidbernat