praw:在Subreddit中获取随机帖子的身体和标题

发布于 2025-02-03 22:16:51 字数 526 浏览 3 评论 0原文

我正在尝试将标题与给定子列数随机帖子的内容(正文)一起打印出来。我一直在互联网上四处寻找如何做到这一点,但我似乎无法弄清楚。

我在这里写了一些东西,可以随机提交。

import praw

reddit = praw.Reddit(
    client_id = "",
    client_secret = "",
    password = "",
    user_agent = "",
    username = "",
)

random_submission = reddit.subreddit('playboicarti').random()

print(random_submission)

示例输出:

v206wo

这里是从称为playboicarti的子reddit的随机提交的ID。

有人知道我应该如何使用ID打印出标题和该提交内容的内容吗?

I'm trying to print out the title along with the content (body) of a random post on a given subreddit. I've been looking around all over the internet on how to do this, but I cant seem to figure it out.

I've written something down here that gets the id of a random submission.

import praw

reddit = praw.Reddit(
    client_id = "",
    client_secret = "",
    password = "",
    user_agent = "",
    username = "",
)

random_submission = reddit.subreddit('playboicarti').random()

print(random_submission)

Example output:

v206wo

Here it's getting the id of a random submission from the subreddit called playboicarti.

Does anyone know how I'm supposed to print out the title and the content of that submission using the id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2025-02-10 22:16:51

您可以同时访问标题属性和url属性,打印标题并在浏览器中打开URL?

import webbrowser 
import praw

reddit = praw.Reddit(
    client_id = "",
    client_secret = "",
    password = "",
    user_agent = "",
    username = "",
)

random_submission = reddit.subreddit('playboicarti').random()
print(random_submission.title)
webbrowser.open(random_submission.url)

关于url属性的好处是,它将遵循链接,或在自我/文本帖子的情况下打开帖子。

如果要打印文本帖子的上下文或打开链接帖子,则可以访问自读文本属性,或像这样打开链接:

import webbrowser
import praw

reddit = praw.Reddit(
    client_id = "",
    client_secret = "",
    password = "",
    user_agent = "",
    username = "",
)

random_submission = reddit.subreddit('playboicarti').random()
print(random_submission.title)

if random_submission.selftext:
    print(random_submission.selftext)
else:
    # a link post will return a blank string ('falsy'), so open the link
    webbrowser.open(random_submission.url)

You can access both the title attributes and the url attributes, print the title and open the url in a browser perhaps?

import webbrowser 
import praw

reddit = praw.Reddit(
    client_id = "",
    client_secret = "",
    password = "",
    user_agent = "",
    username = "",
)

random_submission = reddit.subreddit('playboicarti').random()
print(random_submission.title)
webbrowser.open(random_submission.url)

The nice thing about the url attribute is that it will follow the link, or open the post in the case of a self/text post.

If you wanted to print the context of a text post OR open a link post, you can access the selftext attribute, or open the link like so:

import webbrowser
import praw

reddit = praw.Reddit(
    client_id = "",
    client_secret = "",
    password = "",
    user_agent = "",
    username = "",
)

random_submission = reddit.subreddit('playboicarti').random()
print(random_submission.title)

if random_submission.selftext:
    print(random_submission.selftext)
else:
    # a link post will return a blank string ('falsy'), so open the link
    webbrowser.open(random_submission.url)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文