Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream tweets using tweepy (python) using emoji

I am trying to create an application in python (using tweepy apis) to listen for tweets that include Emojis.

I would like to retrieve all tweets that include the Emoji: 😃, 'U+1F603', '\xF0\x9F\x98\x83'.

The problem is trying to set up the listener to listen for these tweets.

I am using Spyder and Python 2.7

#Set twitter stream
twitterStream = Stream(auth, listener())
twitterStream.filter(track=[tracker], languages = ["en"], stall_warnings = True, async=True)

This is my code that sets up the stream. This seems to work with any text apart from emojis.

I have tried:

tracker = "\xF0\x9F\x98\x83"
tracker = "U+1F603"

I cannot paste the emoji into the IDE as it turns it into bytes and the above code will listen for the text (bytes or unicode) instead of the emoji itself.

Does any one have any recommendations?

like image 207
brian4342 Avatar asked Mar 23 '26 23:03

brian4342


1 Answers

After a lot of research the answer seems quite obvious so I will post it encase others get stuck on a simular problem.

The problem lies with how you handle unicode characters. While some emoji's are 5 characters long they need to be changed.

stream.filter(track=[u"\u1F602"])

This will need to be changed to:

stream.filter(track=[u"\U0001F602"])

So replace 'u' with 'U000'

I also forgot to surround the unicode with U"\"

like image 184
brian4342 Avatar answered Mar 25 '26 13:03

brian4342



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!