📌  相关文章
📜  Python PRAW – 获取 Reddit 上发表评论的时间

📅  最后修改于: 2022-05-13 01:55:13.418000             🧑  作者: Mango

Python PRAW – 获取 Reddit 上发表评论的时间

在 Reddit 中,我们可以对任何提交发表评论,也可以对评论发表评论以创建评论线程。在这里,我们将看到如何获取使用 PRAW 发布评论的确切时间。我们将使用Comment类的created_utc属性来获取评论发布时的 Unix 时间。

示例 1:考虑以下评论:

评论的ID是:fvib7aw

# importing the module
import praw
from datetime import datetime 
  
# initialize with appropriate values
client_id = ""
client_secret = ""
username = ""
password = ""
user_agent = ""
  
# creating an authorized reddit instance
reddit = praw.Reddit(client_id = client_id, 
                     client_secret = client_secret, 
                     username = username, 
                     password = password,
                     user_agent = user_agent) 
  
# the ID of the comment
comment_id = "fvib7aw"
  
# instantiating the Comment class
comment = reddit.comment(comment_id)
  
# fetching the Unix time
unix_time = comment.created_utc 
    
print("The comment was posted on Unix time : " +
      str(unix_time)) 
    
# converting the Unix time 
print("The comment was posted on : " +
      str(datetime.fromtimestamp(unix_time))) 

输出 :

The comment was posted on Unix time : 1592712950.0
The comment was posted on : 2020-06-21 09:45:50

示例 2:考虑以下评论:

评论的ID是:fv9qvgo

# importing the module
import praw
from datetime import datetime 
  
# initialize with appropriate values
client_id = ""
client_secret = ""
username = ""
password = ""
user_agent = ""
  
# creating an authorized reddit instance
reddit = praw.Reddit(client_id = client_id, 
                     client_secret = client_secret, 
                     username = username, 
                     password = password,
                     user_agent = user_agent) 
  
# the ID of the comment
comment_id = "fv9qvgo"
  
# instantiating the Comment class
comment = reddit.comment(comment_id)
  
# fetching the Unix time
unix_time = comment.created_utc 
    
print("The comment was posted on Unix time : " +
      str(unix_time)) 
    
# converting the Unix time 
print("The comment was posted on : " +
      str(datetime.fromtimestamp(unix_time))) 

输出 :

The comment was posted on Unix time : 1592513850.0
The comment was posted on : 2020-06-19 02:27:30