Interact with discourse from Python?

你不总是需要使用别人的界面。

通过内置的 requests 库来做这件事并不太麻烦,如果你使用像 Postman(我已经设置好了)这样的工具,你可以将我们的 API 规范 导入到其中,然后让它生成代码:

然后根据你的需求进行调整:

import json
import pandas
import requests

url = "https://try.discourse.org/categories.json?include_subcategories=false"

payload = {}
headers = {
  'Accept': 'application/json'
}

response = requests.request("GET", url, headers=headers, data=payload)
response_data = json.loads(response.text)
data = pandas.DataFrame(response_data['category_list']['categories']).set_index('id')

现在我有了类别信息:

            name   color text_color  ... uploaded_background_dark                                             topics can_vote
id                                   ...                                                                                     
5        general  25AAE2     FFFFFF  ...                     None  [{'fancy_title': 'Testing dulu ya jangan di hi...      NaN
4         videos  258af1     FFFFFF  ...                     None  [{'fancy_title': 'Ikan ganteng yang’&rdq...      NaN
86      calendar  12A89D     FFFFFF  ...                     None  [{'fancy_title': 'Category Calendar demo topic...      NaN
2           tech     444     FFFFFF  ...                     None  [{'fancy_title': 'Poll: What’s your pref...      NaN
1      discourse  00B355     FFFFFF  ...                     None  [{'fancy_title': 'Welcome to our demo!', 'id':...      NaN
53  Topic Voting  F7941D     FFFFFF  ...                     None  [{'fancy_title': 'Is this topic worth voting f...     True
6         gaming  800080     FFFFFF  ...                     None  [{'fancy_title': 'Impressions Games City Build...      NaN
8         movies  B22222     FFFFFF  ...                     None  [{'fancy_title': 'What’s your all-time f...      NaN
9         sports  0000FF     FFFFFF  ...                     None  [{'fancy_title': 'Modernizing the antiquated b...      NaN

[9 rows x 45 columns]

如果你想访问受保护的信息,你需要使用 API 密钥

6 个赞