我从tweepy收集的推文不会保存到CSV文件中?

2024-03-29 13:03:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一篇社会数据科学研究论文,我现在所做的就是下载一些推文并将它们放入CSV文件中。但每次执行此操作时,脚本都会执行,但打开时CSV文件为空:

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)

Tags: csvthetotokenauthapisearchsecret
1条回答
网友
1楼 · 发布于 2024-03-29 13:03:25

您可以尝试使用以下不同的方式:

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

相关问题 更多 >