我希望以特定推文的标准格式获取日期和时间。 这是我正在使用的以下代码: -
import tweepy
import time
import datetime
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
import string
from nltk.corpus import stopwords
from nltk.tokenize import TweetTokenizer
consumer_key = '' #your consumer key
consumer_secret = '' # your consumer secret
access_token = '' # your access token
access_secret = '' # your access secret
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
class MyListener(StreamListener):
def on_data(self, data):
try:
tweet=data.split('","text":"')[1].split('","source":"')[0]
saveThis=str(time.time())+'::'+tweet
with open('twitDB.csv', 'a') as f:
f.write(saveThis)
f.write('\n')
f.close()
return True
except BaseException as e:
print(str(e))
return True
def on_error(self, status):
print(status)
return True
twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(track=['cars'], languages=['en'])
这是我从中得到的: -
1495596971.6034188::automotive auto ebc greenstuff 6000 series supreme truck
and suv brake pads dp61603 https:\/\/t.co\/jpylzjyd5o cars\u2026
https:\/\/t.co\/gfsbz6pkj7""display_text_range:[0140]source:""\u003ca
href=\""https:\/\/dlvrit.com\/\""
rel=\""nofollow\""\u003edlvr.it\u003c\/a\u003e"""
"1495596972.330948::new free stock photo of city cars road
https:\/\/t.co\/qbkgvkfgpp""display_text_range:[0"
"1495596972.775966::ebay: 1974 volkswagen beetle - classic 1952 custom
conversion extremely rare 1974 vw beetle\u2026\u2026
https:\/\/t.co\/wdsnf2pmo7""display_text_range:[0140]source:""\u003ca
href=\""https:\/\/dlvrit.com\/\""
rel=\""nofollow\""\u003edlvr.it\u003c\/a\u003e"""
我希望标准格式的日期和时间而不是“1495596971.6034188”。 你知道我该怎么办?
答案 0 :(得分:1)
以下是将unix时间戳转换为可读日期的基本示例:
import datetime
print(
datetime.datetime.fromtimestamp(
int("1495596971")
).strftime('%Y-%m-%d %H:%M:%S')
)
>>2017-05-24 03:36:11
你需要用“。”拆分()“”1495596971.6034188“并使用第一个索引[0]