首页 > 解决方案 > 导出数组内容时出现 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])

标签: pythontweepy

解决方案


您的函数 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)

推荐阅读