Python - Tweepy,检索created_at

时间:2015-12-18 14:40:37

标签: python api twitter

我试图检索推文以及创建时间的日期。这是我的代码到目前为止的样子:

import tweepy
import json
import urllib
import sys
import datetime

from tweepy import OAuthHandler

user = "billgates"
count = 1

def twitter_fetch(screen_name = user,maxnumtweets=count):


    consumer_token = 'INSERT CONSUMER TOKEN' 
    consumer_secret = 'INSERT CONSUMER SECRET'
    access_token = 'INSERT ACCESS TOKEN'
    access_secret = 'INSERT ACCESS SECRET'

    auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
    auth.set_access_token(access_token,access_secret)

    api  = tweepy.API(auth)


    for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count): 
        print status.text+'\n'


if __name__ == '__main__':
    twitter_fetch(user,count)

我知道我可能需要使用" created_at"来调用日期,但我并不确定将其放在何处以便检索它。有谁知道我怎么做到这一点?

2 个答案:

答案 0 :(得分:2)

正如Wander Nauta所说,改变界限:

for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count): 
        print status.text+'\n'

为:

for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count): 
        print status.text + ' ' + str(status.created_at) + '\n'

应该打印推文以及创建推文的时间和日期。

答案 1 :(得分:1)

我不确定这是否正是您正在寻找的,但此代码应该有效:

import tweepy
import json
import urllib
import sys
import datetime

from tweepy import OAuthHandler

user = "billgates"
count = 1

def twitter_fetch(screen_name = user,maxnumtweets=count):


    consumer_token = 'INSERT CONSUMER TOKEN' 
    consumer_secret = 'INSERT CONSUMER SECRET'
    access_token = 'INSERT ACCESS TOKEN'
    access_secret = 'INSERT ACCESS SECRET'

    auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
    auth.set_access_token(access_token,access_secret)

    api  = tweepy.API(auth)


    for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count): 
            print status.text+'\n'
            print status.created_at


if __name__ == '__main__':
    twitter_fetch(user,count)

我刚刚将“print status.created_at”行添加到您的代码中,该代码将打印创建推文的日期和时间(类型为datetime.datetime)。