Tweepy-使用.usertimeline排除转推

问题描述 投票:0回答:1

我正在尝试将特定帐户的所有推文都转换为CSV。我的以下内容正常运行,但现在我试图从.usertimeline

中提取的内容中排除转发
alltweets = []
new_tweets = api.user_timeline(screen_name = screen_name,count=200)

alltweets.extend(new_tweets)

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

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

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


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

#transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.replace('\n',' ').encode('utf-8'), tweet.retweet_count, tweet.favorite_count, tweet.retweeted] for tweet in alltweets]

我已经尝试使用以下方法,但到目前为止没有解决方案。我收到以下错误:

AttributeError: 'list' object has no attribute 'text'
for tweet in outtweets:
    if ('RT @' not in tweet.text):
#write the csv
        with open('%s_tweets.csv' % screen_name, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(["id","created_at","text", "retweet_count", "favorite_count", "retweeted"])
            writer.writerows(outtweets)

pass
python tweepy
1个回答
1
投票

outtweets中的每个项目都是具有以下属性的列表:

0 - id_str
1 - created_at
2 - tweet_text
3 - retweet_count
4 - favorite_count
5 - retweeted

这是tweet [2],您需要从我在这里看到的内容中获取。所以你应该使用:

#write the csv
 with open('%s_tweets.csv' % screen_name, 'w') as f:
   writer = csv.writer(f)
   writer.writerow(["id","created_at","text", "retweet_count", "favorite_count", "retweeted"])
   for tweet in outtweets:
     if ('RT @' not in tweet[2]):
        writer.writerow(tweet)
© www.soinside.com 2019 - 2024. All rights reserved.