如何在tweepy中使用since_id和max_id?

时间:2014-09-15 10:26:30

标签: python twitter tweepy

我正在尝试使用since_id和max_id来推送推文。 since_id的问题在于它正确地打印了我的eclipse控制台中的所有推文,但是当我尝试将它逐行存储为csv文件时,它并没有返回所有推文。我尝试运行它4-5次,但每次我得到不同数量的推文.max_id的问题是它没有运行。我的代码如下,代码后面包含回溯(对于since_id,我只是用since_id替换max_id)

#!/usr/bin/python
import tweepy
import csv 
from datetime import *
import time
access_token = ''
access_secret = ''
consumer_key = ''
consumer_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
#data = api.get_user('abhi1868sharma')#'mishra1_P_K'

csvFile = open('a.csv','a')

csvWriter = csv.writer(csvFile, delimiter=',')#, tweet.favourited
i = 1
tweets = tweepy.Cursor(api.user_timeline, id = '', max_id = 510064587115225000).items()
while True:
    try:
        for tweet in tweets:
            csvWriter.writerow([i, tweet.retweet_count, tweet.favorite_count, str(tweet.id), tweet.created_at.hour,tweet.created_at.minute,tweet.created_at.weekday(),tweet.created_at.day,tweet.created_at.month,tweet.created_at.year,tweet.created_at ,tweet.text.encode('utf8'), tweet.user.id, tweet.geo, tweet.in_reply_to_user_id, tweet.in_reply_to_status_id_str, tweet.place, tweet.retweeted, tweet.truncated, tweet.source])
            print i
            i+=1


    except tweepy.TweepError:
        time.sleep(60 * 15) 
        continue
    except StopIteration:
        break

csvFile.close()  

这是我对max_id的追溯(对于since_id,它没有抛出任何错误)

for tweet in tweets:
File "C:\Python27\lib\site-packages\tweepy\cursor.py", line 181, in next
    self.current_page = self.page_iterator.next()
  File "C:\Python27\lib\site-packages\tweepy\cursor.py", line 99, in next
    data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
TypeError: _call() got multiple values for keyword argument 'max_id'

另外一件事是,当我将文件存储在csv文件中时,我的tweet_ids被四舍五入。有没有办法解决它。我用于since_id和max_id的tweet_id也是tweet id。

0 个答案:

没有答案