Экспорт данных из Tweepy в CSV (ошибка: недоступно для записи) - PullRequest
0 голосов
/ 16 марта 2020

Я использую Tweepy для получения аналитики из твитов указанного пользователя. Я хочу взять аналитику твитов и экспортировать их в файл CSV, однако я получаю сообщение об ошибке: «io.UnsupportedOperation: not writeable»

Вот мой код, я, очевидно, оставил свои токены Twitter из это:)

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")

users = ["30SecFights"]

def timeline(username):
    tweets = api.user_timeline(screen_name=username, count = '50', 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(users[0])

# Tweepy
def probe_to_csv(tweets, path):
    with open(path, 'r', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(['id', 'created', 'hashtags', 'text', 'retweet_count', 'fav_count', 'mentions', 'screen_names', 'words'])
        writer.writerows(tweets)

path = "/Users/Slemming/Desktop/CAP/analytics.csv"

probe_to_csv(timeline, path)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...