简体   繁体   中英

The tweets I'm collecting from tweepy won't save into the CSV file?

I'm trying to do a social data science research paper and all I'm trying to do right now is download some tweets and put them into a CSV file. Every time I do this though, the script executes but the CSV file is empty when opened:

import tweepy
import csv

consumer_key=""
consumer_secret=""

access_token=""
access_token_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)


search_words = "leftists -filter:retweets"
date_since = "2021-01-18"

# how does this algorithm determine what tweets are printed?

tweets = tweepy.Cursor(api.search, q=search_words, lang="en",since=date_since).items(100)

# open and create a file to append the data to
tweets = str(tweets)
csvFile = open(tweets+'.csv', 'a')
# use the csv file
# loop through the tweets variable and add them to the CSV file
with open('tweets.csv', 'w') as csvfile:
    csvWriter = csv.writer(csvFile)
    for tweet in tweepy.Cursor(api.search,q=tweets,count=100,lang="en",since=date_since, tweet_mode='extended').items(10):
        print (tweet.created_at, tweet.full_text)
        csvWriter.writerow([tweet.created_at, tweet.full_text.encode('utf-8')])
        csvFile.close()

print ("Scraping finished and saved to "+tweets+".csv")
for tweet in tweets:
    print(tweet.text)

You can try to use this different way:

import xlwt

book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Sheet 1")

counter = 0
for tweet in tweepy.Cursor(api.search,q=tweets,count=100,lang="en",since=date_since, tweet_mode='extended').items(10):
    sheet1.write(counter, 0, str(tweet.created_at, tweet.full_text.encode('utf-8')))
    counter += 1



book.save("trial.csv")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM