如何使用 Twython 获取所有直接消息?

How to fetch all the direct messages using Twython?

我正在尝试获取我帐户中的所有直接消息。但我尝试使用游标进行抓取。但是我收到类似

的错误
TypeError: cursor() missing 1 required positional argument: 'function'

我的代码如下。

from twython import Twython

APP_KEY = 'KEY'
APP_SECRET = 'SECRET'
ACCESS_TOKEN = 'TOKEN'
ACCESS_SECRET = 'SECRET'

twitter = Twython(APP_KEY,APP_SECRET,ACCESS_TOKEN,ACCESS_SECRET)

for tweet in Twython.cursor(twitter.get_direct_messages(),since_id=1,count=100):
print (tweet)

请帮忙解决这个问题。

很确定您需要在 twitter 对象上调用 .cursor 方法,而不是在 Twython class 上。

所以试试

for tweet in twitter.cursor(twitter.get_direct_messages(),since_id=1,count=100):
    print (tweet)

具体来说,这对我有用:

results = twitter.cursor(twitter.get_direct_messages,count=2)
for result in results:
     print result['text']

您也可以直接从 get_direct_messages 的 return 访问它们:

messages = twitter.get_direct_messages(count=200)

for message_id in messages:
    id = message_id['id']
    text = message_id['text']
    print text