Home:ALL Converter>Saving Urdu tweets from tweepy in CSV file

Saving Urdu tweets from tweepy in CSV file

Ask Time:2018-07-14T01:22:42         Author:Tayyab Vohra

Json Formatter

I am trying to save only Urdu language tweets using tweepy in python. I am using 3.6 version of python. The data in Urdu language is not saving in the file if I open the file I am only been able to see only the usernames, not tweets in Urdu. this is my code

The same code is working for the English language.

import re
import io
import csv
import tweepy
from tweepy import OAuthHandler
#from textblob import TextBlob


consumer_key = "xxxxxxxxxxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxxx"
access_key = "xxxxxxxxxxxxxxxxxxxxx"
access_secret = "xxxxxxxxxxxxxxxxxxxx"


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
auth.set_access_token(access_key, access_secret)
# create tweepy API object to fetch tweets
api = tweepy.API(auth)




def get_tweets(query, count = 300):

    # empty list to store parsed tweets
    tweets = []
    target = io.open("newsfileurdu.csv", 'w', encoding='utf-8')
    # call twitter api to fetch tweets
    q=str(query)
    a=str(q+" اردو")
    b=str(q+" خبریں")
    c=str(q+" خبریں اردو")
    fetched_tweets = api.search(a, count = count)+ api.search(b, count = count)+ api.search(c, count = count)
    # parsing tweets one by one
    print(len(fetched_tweets))

    for tweet in fetched_tweets:

        # empty dictionary to store required params of a tweet
        parsed_tweet = {}
        # saving text of tweet
        parsed_tweet['text'] = tweet.text
        if "http" not in tweet.text:
            line = re.sub("[^A-Za-z]", " ", tweet.text)
            target.write(line+"\n")
    return tweets

    # creating object of TwitterClient Class
    # calling function to get tweets
tweets = get_tweets(query ="", count = 20000)

Author:Tayyab Vohra,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51330086/saving-urdu-tweets-from-tweepy-in-csv-file
yy