brian4342
brian4342

Reputation: 1253

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?

Upvotes: 3

Views: 2532

Answers (1)

brian4342
brian4342

Reputation: 1253

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"\"

Upvotes: 5

Related Questions