python - 导出数组内容时出现 Tweepy 错误
问题描述
我正在寻找提取推文并将它们写入 CSV 文件,但是,我无法弄清楚如何让它生成文件。我正在使用 Tweepy 来提取推文。我希望 CSV 文件包含以下单元格:用户、日期、推文、喜欢、转发、总数、英文率、评级、推文 ID
import tweepy
import csv
auth = tweepy.OAuthHandler("", "")
auth.set_access_token("", "")
api = tweepy.API(auth)
try:
api.verify_credentials()
print("Authentication OK")
except:
print("Error during authentication")
def timeline(username):
tweets = api.user_timeline(screen_name=username, count = '100', tweet_mode="extended")
for status in (tweets):
eng = round(((status.favorite_count + status.retweet_count)/status.user.followers_count)*100, 2)
if (not status.retweeted) and ('RT @' not in status.full_text) and (eng <= 0.02):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Low' + ',Tweet ID: ' + str(status.id))
elif (not status.retweeted) and ('RT @' not in status.full_text) and (0.02 < eng <= 0.09):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Good' + ',Tweet ID: ' + str(status.id))
elif (not status.retweeted) and ('RT @' not in status.full_text) and (0.09 < eng <= 0.33):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: High' + ',Tweet ID: ' + str(status.id))
elif (not status.retweeted) and ('RT @' not in status.full_text) and (0.33 < eng):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Very High' + ',Tweet ID: ' + str(status.id))
tweet = timeline("twitter")
with open('tweet.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([tweet])
解决方案
您的函数 get_tweets 没有返回值,但您试图从该函数中检索一个值,这将导致 None。此外,tweet 值看起来也将是字符串列表。来自 csv.writer 的 writerow 方法应该获取项目列表而不是列表列表。我已修改您的代码以解决这些问题。让我知道它是否有效。
def get_tweets(username):
tweets = api.user_timeline(screen_name=username, count=100)
tweets_for_csv = [tweet.text for tweet in tweets]
print(tweets_for_csv)
return tweets_for_csv
tweet = get_tweets("fazeclan")
with open('tweet.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(tweet)
推荐阅读
- java - 数据未从 java spring 微服务中的 postgres 显示
- openstack - OpenStack 是否在不支持巨型帧的物理网络上支持巨型帧内部网络?
- c# - 在 aspnet 核心中使用固定的用户名和密码创建会话
- excel - 验证用户表单的文本框
- amazon-web-services - 将旧 RDS 流量重定向到 AWS 中的新 RDS
- abap - 如何将旧 HR INCLUDE 添加到本地课程中?
- ios - 在 Pod 中停止本地化
- python - 我在进行情绪分析时遇到 TypeError,我该如何解决这个问题?
- c# - 引用的 SDK“SQLite.UWP.2015”不支持正在构建的项目的处理器架构“任何 CPU”。在 Visual Studio 2017 中
- javascript - 仅第一次出现替换函数