##################################### PRAW: Python Reddit API 封装 #####################################
.. image:: https://img.shields.io/pypi/v/praw.svg :alt: 最新 PRAW 版本 :target: https://pypi.python.org/pypi/praw
.. image:: https://img.shields.io/pypi/pyversions/praw :alt: 支持的 Python 版本 :target: https://pypi.python.org/pypi/praw
.. image:: https://img.shields.io/pypi/dm/praw :alt: PyPI - 下载量 - 每月 :target: https://pypi.python.org/pypi/praw
.. image:: https://github.com/praw-dev/praw/actions/workflows/ci.yml/badge.svg?event=push :alt: GitHub Actions 状态 :target: https://github.com/praw-dev/praw/actions/workflows/ci.yml
.. image:: https://api.securityscorecards.dev/projects/github.com/praw-dev/praw/badge :alt: OpenSSF Scorecard :target: https://api.securityscorecards.dev/projects/github.com/praw-dev/praw
.. image:: https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg :alt: Contributor Covenant :target: https://github.com/praw-dev/.github/blob/main/CODE_OF_CONDUCT.md
.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white :alt: pre-commit :target: https://github.com/pre-commit/pre-commit
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json :alt: Ruff :target: https://github.com/astral-sh/ruff
PRAW,即“Python Reddit API Wrapper”的缩写,是一个允许简单访问 Reddit API 的 Python 包。PRAW 旨在易于使用,并在内部遵循所有
Reddit's API rules <https://github.com/reddit/reddit/wiki/API>_。使用 PRAW 时,无需在代码中引入 sleep 调用。为你的客户端提供合适的用户代理即可。
.. _installation:
安装
PRAW 支持 Python 3.10+。安装 PRAW 的推荐方式是使用 uv <https://docs.astral.sh/uv/>_。
.. code-block:: bash
uv add praw
或者,可以通过 pip <https://pypi.python.org/pypi/pip>_ 安装 PRAW:
.. code-block:: bash
pip install praw
要安装 PRAW 的最新开发版本,请运行以下命令:
.. code-block:: bash
pip install --upgrade https://github.com/praw-dev/praw/archive/main.zip
有关安装 Python 和 pip 的说明,请参阅“The Hitchhiker's Guide to Python”
Installation Guides <https://docs.python-guide.org/en/latest/starting/installation/>_。
快速入门
假设你已经有脚本类型 OAuth 应用的凭据,你可以像这样实例化一个 PRAW 实例:
.. code-block:: python
import praw
reddit = praw.Reddit(
client_id="CLIENT_ID",
client_secret="CLIENT_SECRET",
password="PASSWORD",
user_agent="USERAGENT",
username="USERNAME",
)
使用 reddit 实例,你可以与 Reddit 进行交互:
.. code-block:: python
# Create a submission to r/test
reddit.subreddit("test").submit("Test Submission", url="https://reddit.com")
对已知帖子发表评论
submission = reddit.submission(url="https://www.reddit.com/comments/5e1az9")
submission.reply("Super rad!")
# 回复受管理社区每周热门帖子的第一条评论
submission = next(reddit.subreddit("mod").top(time_filter="week"))
submission.comments[0].reply("An automated reply")
# 输出首页前 256 个条目的分数
for submission in reddit.front.hot(limit=256):
print(submission.score)
# 获取 r/test 的管理员列表
for moderator in reddit.subreddit("test").moderator():
print(moderator)
请参阅 PRAW 的 documentation <https://praw.readthedocs.io/>_ 以获取更多关于
使用 PRAW 可以做什么的示例。
Discord 机器人和异步环境
如果您计划在异步环境中使用 PRAW,(例如 discord.py、asyncio)强烈
建议使用 Async PRAW <https://asyncpraw.readthedocs.io/>_。它是
PRAW 的官方异步版本,其用法相似,并具有与 PRAW 相同的功能。
PRAW 讨论与支持
对于 Python 新手,或者自认为是 Python 初学者的人,
请考虑在 r/learnpython <https://www.reddit.com/r/learnpython>_ 子版块上提问。那里有
很棒的人可以帮助解答通用的 Python 问题和简单的 PRAW 相关问题。
否则,有几个官方场所可以询问关于 PRAW 的问题:
r/redditdev <https://www.reddit.com/r/redditdev>_ 是 Reddit 上提问
PRAW 相关问题最佳的地方。该 subreddit 用于所有与 Reddit API 相关的讨论,因此
请在提交中标注 [PRAW]。请先在该 subreddit 中进行搜索,
查看是否有人提出过类似的问题。
实时聊天可以通过 PRAW Slack Organization <https://join.slack.com/t/praw/shared_invite/zt-3d5lbcs51-VB1xRhRok0OZ1vyYquWb2A>_
进行(如果该邀请链接已过期,请创建一个 issue)。
除非贡献者另有说明,否则请勿通过 Reddit、电子邮件或 Slack 直接联系任何贡献者。我们强烈鼓励大家帮助他人解答 他们的问题。
请先搜索以确保没有已提交类似的
issue,然后在 GitHub <https://github.com/praw-dev/praw/issues>_ 上以 issue 的形式提交 bug 和功能请求。如果已存在此类 issue,请给予
点赞反应。包含额外信息的 issue 评论当然也是受欢迎的。
.. note::
本项目附带发布了一份 `Contributor Code of Conduct
<https://github.com/praw-dev/.github/blob/main/CODE_OF_CONDUCT.md>`_。通过
参与本项目,您同意遵守其条款。
文档
PRAW 的文档位于 https://praw.readthedocs.io/。
历史
August 2010 <https://github.com/praw-dev/praw/commit/efef08a4a713fcfd7dfddf992097cf89426586ae>_:
Timothy Mellor 创建了一个名为 reddit_api 的 github 项目。
March 2011 <https://github.com/praw-dev/praw/commit/ebfc9caba5b58b9e68c77af9c8e53f5562a2ee64>_:
Python 包 reddit 已注册并上传到 pypi。
December 2011 <https://github.com/praw-dev/praw/commit/74bb962b3eefe04ce6acad88e6f53f43d10c8803>_:
Bryce Boe 接管了 reddit 包的维护工作。
2012 年 6 月 <https://github.com/praw-dev/praw/commit/adaf89fe8631f41ab9913b379de104c9ef6a1e73>_:
Bryce 将项目重命名为 PRAW,并且仓库被迁移到 GitHub 上新创建的
praw-dev 组织。
2016 年 2 月 <https://github.com/praw-dev/praw/commit/252083ef1dbfe6ea53c2dc99ac235b4ba330b658>_:
Bryce 开始开发 PRAW4,即 PRAW 的完全重写版本。
许可证
PRAW 的源代码(v4.0.0+)在 Simplified BSD License <https://github.com/praw-dev/praw/blob/0860c11a9309c80621c267af7caeb6a993933744/LICENSE.txt>_ 下提供。
- Copyright ©, 2016, Bryce Boe
PRAW 的早期版本在 GPLv3 <https://github.com/praw-dev/praw/blob/0c88697fdc26e75f87b68e2feb11e101e90ce215/COPYING>_ 下发布。