简体   繁体   English

Python - Tweepy,检索created_at

[英]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. 我知道我可能需要使用“created_at”来调用日期,但我不确定在哪里放置它以便检索它。 Does anyone know how I can do this? 有谁知道我怎么做到这一点?

As Wander Nauta said, changing the lines: 正如Wander Nauta所说,改变线条:

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

to: 至:

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

should print out the tweet along with the time and date of the creation of the tweet. 应该打印推文以及创建推文的时间和日期。

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). 我刚刚在您的代码中添加了“print status.created_at”行,它将打印创建推文的日期和时间(类型为datetime.datetime)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM