Python PRAW – 在 Reddit 中获取评论的永久链接
在 Reddit 中,我们可以对任何提交发表评论,也可以对评论发表评论以创建评论线程。每个评论都有一个附属的永久静态超链接。在这里,我们将看到如何使用 PRAW 获取评论的永久链接。我们将使用Comment
类的permalink
属性来获取评论的永久链接。
示例 1:考虑以下评论:
评论的ID是:fvib7aw
# importing the module
import praw
# 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 permalink attribute
permalink = comment.permalink
print("The permalink of the comment is : \n" + permalink)
print("\nThe complete URL of the comment is : \n" +
"https://www.reddit.com/" + permalink)
输出 :
The permalink of the comment is :
/r/aww/comments/hczt0c/i_got_adopted_by_this_beautiful_pup_this_weekend/fvib7aw/
The complete URL of the comment is :
https://www.reddit.com//r/aww/comments/hczt0c/i_got_adopted_by_this_beautiful_pup_this_weekend/fvib7aw/
示例 2:考虑以下评论:
评论的ID是:fv9qvgo
# importing the module
import praw
# 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 permalink attribute
permalink = comment.permalink
print("The permalink of the comment is : \n" + permalink)
print("\nThe complete URL of the comment is : \n" +
"https://www.reddit.com/" + permalink)
输出 :
The permalink of the comment is :
/r/redditdev/comments/hbjhnk/reddit_announces_an_upcoming_overhaul_of_how_bots/fv9qvgo/
The complete URL of the comment is :
https://www.reddit.com//r/redditdev/comments/hbjhnk/reddit_announces_an_upcoming_overhaul_of_how_bots/fv9qvgo/
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。