我正在做一篇社会数据科学研究论文,我现在所做的就是下载一些推文并将它们放入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)
您可以尝试使用以下不同的方式:
相关问题 更多 >
编程相关推荐