导入
PRAW 简介
学习如何使用 Python 封装的 Reddit API。
| 参数 | 类型 | 必需性 | 描述 |
|---|---|---|---|
| user_agent | 字符串 | 必需 | user_agent 用于标识网络请求的来源。我们需要一个唯一且具有描述性的 user_agent。 |
| refresh_token | 字符串 | 必需 | 刷新令牌是一串字符,可用于获取经过身份验证的 Reddit 实例。 |
| client_id | 字符串 | 必需 | 这是一串随机字符,用于用户身份验证。 |
| client_secret | 字符串 | 必需 | 这是一串随机字符,用于用户身份验证。 |
1import praw 2 3reddit = praw.Reddit( 4 client_id="{{CLIENT_ID}}", 5 client_secret="{{CLIENT_SECRET}}", 6 refresh_token="{{REFRESH_TOKEN}}", 7 user_agent="testscript by u/{{USERNAME}}" 8) 9 10print(reddit.read_only)
1import praw 2import json 3 4reddit = praw.Reddit( 5 client_id="{{CLIENT_ID}}", 6 client_secret="{{CLIENT_SECRET}}", 7 refresh_token="{{REFRESH_TOKEN}}", 8 user_agent="testscript by u/{{USERNAME}}" 9) 10 11try: 12 response = reddit.user.preferences() 13 print(json.dumps(response, indent=4)) 14except Exception as e: 15 print(e)