twitterscraper
twitterscraper copied to clipboard
AttributeError: Tweet instance has no attribute 'encode'
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:
Which Python version are you running? I think this problem should not appear with Python 3
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'
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.
@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
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
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')))
try file.write(str(tweet.text.encode('utf-8')))
I tried but output.txt was full of unreadable byte code.
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
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))