如何从 Stack Exchange API 中获取特定用户提出的所有问题?

How to get all the questions asked by a specific user from the Stack Exchange API?

我正在尝试使用以下代码从 Stack Exchange API 获取给定用户 ID 的所有问题和详细信息:

response = requests.get("http://api.stackexchange.com/2.2/users/2593236/questions?")

但是,我收到此错误消息。

{"error_id":400,"error_message":"site is required","error_name":"bad_parameter"}

任何人都可以帮我解决这个问题并根据用户 ID 检索所有用户提出的问题吗?

错误消息非常清楚:您必须包含一个 site 参数,如 the documentation:

中所述

Each of these methods operates on a single site at a time, identified by the site parameter. This parameter can be the full domain name (ie. "whosebug.com"), or a short form identified by api_site_parameter on the site object.

尝试

http://api.stackexchange.com/2.2/users/2593236/questions?site=whosebug.com

要下载特定用户和堆栈的所有问题或答案,您可以使用:

import requests, traceback, json

all_items = []
user = 2593236
stack = "whosebug.com"
qa = "questions" # or answers

page = 1
while 1:
    u = f"https://api.stackexchange.com/2.2/users/{user}/{qa}?site={stack}&page={page}&pagesize=100"
    j = requests.get(u).json()
    if j:

        all_items += j["items"]

        if not j['has_more']:
            print("No more Pages")
            break
        elif not j['quota_remaining']:
            print("No Quota Remaining ")
            break
    else:
        print("No Questions")
        break

    page+=1


if all_items:
    print(f"How many {qa}? ", len(all_items))
    # save questions/answers to file
    with open(f"{user}_{qa}_{stack}.json", "w") as f:
        f.write(json.dumps(all_items))

Demo