'Python - Tweepy, retrieving the created_at

I'm trying to retrieve tweets and the dates as to when they were created. This is what my code looks like so far:

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)

I know that I presumably need to call the date using "created_at", but I'm not exactly sure where to put this in order to retrieve it. How can I do this?



Solution 1:[1]

I am not sure whether this is exactly what you are looking for, but this code should work:

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)

I just added the line "print status.created_at" to your code, which will print the date and the time the tweets were created at (type is datetime.datetime).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Markus K