skip to Main Content

I’m having a problem collecting Arabic tweets and save them in a CSV file

when I open the CSV file the tweets is like this

enter image description here

here is the code


import tweepy
import csv


# Twitter API credentials

consumer_key = "..."
consumer_secret = ".."
access_key = "..."
access_secret = "...."

auth= tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_key,access_secret)
api= tweepy.API(auth,wait_on_rate_limit=True)


csvFile=open('tweets.csv','a',newline='')
csvWriter=csv.writer(csvFile)
#truncated=False,
for tweet in tweepy.Cursor(api.search,q="اكتئاب",since="2021-01-30",truncated=False,tweet_mode="extended", count=1).items():

    if (not tweet.retweeted) and ('RT @' not in tweet.full_text):
        csvWriter.writerow([tweet.full_text.encode('utf-8-sig')])

please I need your help :'(

2

Answers


  1. Chosen as BEST ANSWER

    I found my answer that if I add these two lines to my code it will fix it

    #coding:utf8
    csvFile=open('tweets.csv','a',newline='',encoding='utf-8-sig')
    

    the source


    1. For the empty lines you get, see this answer:
      • add the parameter newline='' to the open(...) statement
    2. To get the full tweet text (280 chars), use Extended Mode when invoking the API and/or the Cursor()
      • tweet_mode='extended'
      • and use the parameter full_text instead of just text to get the text of each tweet.
      • You’ll also need to handle retweets slightly differently.
    3. For the Full URLs, see this other answer:
      for url in status.entities['urls']:
          links = url['expanded_url']
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search