我试图分析政治推文。
当我运行此代码时:
import tweepy
from tweepy import OAuthHandler
import datetime
consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_secret = '...'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
username = "VP"
startDate = datetime.datetime(2017, 12, 1, 0, 0, 0)
endDate = datetime.datetime(2017, 12, 2, 0, 0, 0)
tweets = []
tmpTweets = api.user_timeline(username)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
while (tmpTweets[-1].created_at > startDate):
print("Last Tweet @", tmpTweets[-1].created_at, "...fetching more")
tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
for tweet in tweets:
print(tweet.created_at)
我明白了:
Last Tweet @ 2017-12-02 13:52:36 ...fetching more
2017-12-01 21:06:35
2017-12-01 12:29:27
2017-12-01 12:27:36
2017-12-01 00:50:17
2017-12-01 00:47:42
2017-12-01 00:25:32
但这是错误的。副总裁在12月1日发了3次推文。这些时间戳似乎提前了4个小时。我如何解决东部时间?
答案 0 :(得分:4)
<强>时区.. 强>
<a-entity camera="userHeight:1.6" look-controls></a-entity>
是UTC(+0000),而.created_at
和startDate
是本地日期时间对象。
一种方法是将endDate
转换为本地,然后比较:
created_at
其中datetime_from_utc_to_local
is defined as:
created_date_local = datetime_from_utc_to_local(tweet.created_at)
if endDate > created_date_local > startDate:
# ...
这只是一种方法,不一定是最好的方法。
根据需要仅打印3条推文:
def datetime_from_utc_to_local(utc_datetime):
now_timestamp = time.time()
offset = datetime.datetime.fromtimestamp(now_timestamp) - datetime.datetime.utcfromtimestamp(now_timestamp)
return utc_datetime + offset