尝试收集过去24小时内的所有推文并将其放入CSV文件中

时间:2016-08-08 17:45:53

标签: python csv twitter tweepy

我试图收集过去24小时内的所有推文,并将它们放入CSV文件

当我这样做时,我得到了

_csv.Error: iterable expected, not datetime.datetime

作为错误

任何人都可以帮忙告诉我如何摆脱这个错误以及可能对代码做出的任何其他改进

def get_all_tweets(screen_name):
# Twitter only allows access to a users most recent 3240 tweets with this method

# authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)

# initialize a list to hold all the tweepy Tweets
alltweets = []    

# make initial request for most recent tweets (20 is the maximum allowed count)
new_tweets = api.home_timeline (screen_name=screen_name, count=20)

# save most recent tweets
alltweets.extend(new_tweets)

# save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

page = 1
deadend = False



while len(new_tweets) > 0:
        print ("getting tweets before %s" % (oldest))

        # all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = api.home_timeline(screen_name=screen_name, count=20, max_id=oldest,  page = page)

        # save most recent tweets
        alltweets.extend(new_tweets)

        # update the id of the oldest tweet less one
        oldest = alltweets[-1].id - 1

        print ("...%s tweets downloaded so far" % (len(alltweets)))

        for tweet in alltweets:

            if (datetime.datetime.now() - tweet.created_at).days < 1:

                 # transform the tweepy tweets into a 2D array that will populate the csv    
                 outtweets = [tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")]

            else:
                 deadend = True
                 return
        if not deadend:
             page += 1
             time.sleep(10)

# write the csv    
with open('%s_tweetsBQ.csv' % screen_name, 'w') as f:
    writer = csv.writer(f)
    writer.writerow(["id", "created_at", "text"])
    writer.writerows(outtweets)
pass

print ("CSV written")

if __name__ == '__main__':
# pass in the username of the account you want to download
get_all_tweets("BQ")

修改

(most recent call last):
 File "C:\Users\Barry\workspace\TwitterTest\Test1\MGo.py", line 77, in    <module>
  get_all_tweets("BQ")
File "C:\Users\Barry\workspace\TwitterTest\Test1\MGo.py", line 70, in get_all_tweets
writer.writerows(outtweets)
 _csv.Error: iterable expected, not datetime.datetime

编辑2

for row in outtweets:
            date_str,time_str, entries_str = row.split()
            #print(a_date,a_time, entries)
            a_time = datetime.strptime(time_str, "%H:%M:%S")
            for e in entries_str.split(','): 
                 # write the csv    
                 with open('%s_tweetsBQ.csv' % screen_name, 'w') as f:
                     writer = csv.writer(f)
                     writer.writerow(["id", "created_at", "text"])
                     writer.writerows(outtweets)
                 pass

1 个答案:

答案 0 :(得分:2)

outtweets只包含单行数据。 writer.writerows()需要一个行列表,即列表列表:

[
  [columns, in, row, 1],
  [columns, in, row, 2],
]

您正在设置outtweets,如下所示:

outtweets = [tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")]

那只是一排。要将此行传递给writerows,您需要将每行数据累积到一个列表中,然后将该列表传递给writerows