Search code examples
pythontwittertweepy

How to get tweets by hashtag, if hashtag contains Non-ASCII character or non English characters in tweepy?


I am fetching tweets through the hashtag if a hashtag is in English or in ASCII characters its work fine. but if I use non-English characters. It produces an error. obviously due to non-ASCII characters in the code. Is there any way to query non-English hashtag. Here is my code.

import tweepy

ckey = ""
csecret = ""
atoken = ""
asecret = ""

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
 'access_token_key':atoken, 'access_token_secret':asecret}
 auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)

TweetFetch = tweepy.Cursor(api.search, q='Lahore').items(10)

This works fine. because of my hashtag q=Lahore is in English.

import tweepy

ckey = ""
csecret = ""
atoken = ""
asecret = ""

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
 'access_token_key':atoken, 'access_token_secret':asecret}
 auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)

TweetFetch = tweepy.Cursor(api.search, q='لاہور').items(10)

if I write a non-English character suppose in Urdu q=لاہور. That produces an error "NON-ASCII character in the file".


Solution

  • I might suggest checking your encoding. Prior to Python 3, ASCII was the default encoding. To change this, place # coding: utf-8 at the beginning of your file, and make sure to save it as UTF-8 in the text editor you are using.