如何解决我从 tweepy 收集的推文不会保存到 CSV 文件中?
我正在尝试撰写一篇社会数据科学研究论文,而我现在要做的就是下载一些推文并将它们放入 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,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)
解决方法
您可以尝试使用这种不同的方式:
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,str(tweet.created_at,tweet.full_text.encode('utf-8')))
counter += 1
book.save("trial.csv")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。