Python& Tweepy - 如何比较和改变时间。

时间:2017-08-02 20:36:07

标签: python datetime datetime-format tweepy

我正在尝试基于twitter句柄集为其他一些代码创建一些约束。

我遇到以下代码的问题,因为:

TypeError: can't compare datetime.datetime to str

似乎即使我最初将Last_Post更改为datetime对象,当我将它与datetime.datetime.today()进行比较时,它也会转换为字符串。是的,我已经检查过以确保Last_post正确转换。我不确定发生了什么。帮助

for handle in handles:
    try:
        user = api.get_user(handle)
        #print json.dumps(user, indent = 4)
        verified = user["verified"]
        name = user['name']
        language = user['lang']
        follower_count = user['followers_count']
        try:
            last_post = user['status']['created_at']
            last_post = datetime.strptime(last_post, '%a %b %d %H:%M:%S +0000 %Y')
        except: 
            last_post = "User has not posted ever"
        location = user['location']

        location_ch = location_check(location)

        if location_ch is not "United States":
            location_output.append(False)
        else:
            location_output.append(True)

        new_sum.append(follower_count)

        if language is not "en":
            lang_output.append(False)
        else:
            lang_output.append(True)

        if datetime.datetime.today() - datetime.timedelta(days=30) > last_post:
            recency.append(False)
        else:
            recency.append(True)

1 个答案:

答案 0 :(得分:1)

我认为您需要将Twitter日期转换为时间戳:

import time

ts = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))