尝试将推文(从流)写入CSV

时间:2019-02-27 02:36:19

标签: csv twitter tweepy

我试图通过跟踪某个主题来流式传输10条推文,在本示例中,我正在跟踪“ Trump”。下面的代码可以正常工作,因为它确实返回了10个状态,但是它仅将其中一种状态写入到csv中,甚至更奇怪的是,它将其写入10x并在csv中的随机部分(例如第400行)中写入。

我在做什么错了?

import setup
import csv
import tweepy
from tweepy import StreamListener


class Streamer(StreamListener):
    def __init__(self):
        super().__init__()
        self.counter = 0
        self.limit = 10

    def on_status(self, status):
        statuses = []

        if status.retweeted or "RT @" in status.text:
            return
        while len(statuses) < self.limit and status.lang == "en":
            self.counter += 1
            print(status)
            statuses.append(status)
            with open("my_csv_file", "a") as f:    
                writer = csv.writer(f)
                for status in statuses:
                    writer.writerow([status.user.screen_name, status.text,   status.created_at, status.user.location,
                                    status.id_str])

        if len(statuses) == self.limit:
            print("*** Limit of " + str(self.limit) + " met ***")
            return False
        if len(statuses) > self.limit:
            streaming.disconnect()


streaming = tweepy.Stream(auth=setup.api.auth, listener=Streamer())

stream_data = streaming.filter(track=["Trump"])

0 个答案:

没有答案