Python 导出数组内容时出现Tweepy错误

Python 导出数组内容时出现Tweepy错误,python,tweepy,Python,Tweepy,我希望提取推文并将其写入CSV文件,但是,我不知道如何使其生成文件。我正在使用Tweepy提取推文。我希望CSV文件包含以下单元格:用户、日期、tweet、喜欢、转发、总数、英语等级、评级、tweet id import tweepy import csv auth = tweepy.OAuthHandler("", "") auth.set_access_token("", "") api = tweepy.API(auth) try: api.verify_credential

我希望提取推文并将其写入CSV文件,但是,我不知道如何使其生成文件。我正在使用Tweepy提取推文。我希望CSV文件包含以下单元格:用户、日期、tweet、喜欢、转发、总数、英语等级、评级、tweet 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])
导入tweepy
导入csv
auth=tweepy.OAuthHandler(“,”)
身份验证设置访问令牌(“,”)
api=tweepy.api(auth)
尝试:
api.verify_凭证()
打印(“身份验证确定”)
除:
打印(“身份验证期间出错”)
def时间表(用户名):
tweets=api.user\u时间线(屏幕名称=用户名,计数='100',tweet\u mode=“扩展”)
关于(推文)中的状态:
eng=轮(((status.favorite\u count+status.retweet\u count)/status.user.followers\u count)*100,2)
如果(not status.retweeted)和('RT@'not in status.full_text)和(eng),您可以查看有关如何在Python中生成csv文件的信息。Quick exmaple:

import csv

with open('some_output.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["field1", "field2", "field3"])

您的函数get_tweets没有返回值,但您正在尝试从该函数中检索一个值,结果为“无”。此外,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)

节目结束时
tmp
的值是多少?还有,csv会有哪些列?您尝试创建csv文件的代码是什么?我看到的只是提取推文文本并打印其内容的代码。目前,输出是一个大字符串,末尾有一个链接。如果可能,我想为每个推文指定一个数字tweet和link是分开的,所以三列中有一个数字,tweet,link。使用
csv
模块读取和写入csv文件。填充
tmp
for
循环可以替换为
tmp=tweets\u for\u csv[:]
因为它只是该列表的一个副本。谢谢!它似乎可以工作,但在大多数情况下,它仍然没有保存到CSV文件。@Lucassleming它至少创建了一个空的CSV文件吗?如果从命令行运行脚本,则可以在与正在运行的脚本不同的位置创建该文件。我得到了它来创建一个空的CSV,是的。但是,代码已经更新,包含了更多的数据,我必须承认我在这一点上有点困惑:)