无论不同的 API 调用如何,程序最终都使用相同的值

Program ends up using the same values regardless of different API calls

我想使用 Steam API 获取 500 条游戏评论。单次API 调用returns 20 条游戏评论。我循环 25 次以获得 500 条评论。我还计算了这些评论中的单词并将每个单词(频率)的出现次数写到文件中。

问题是我文件中的频率是 25 的倍数。因此,对于所有 25 次迭代,一遍又一遍地获取相同的 20 条评论。这意味着相同的 20 条评论被计算了 25 次。

这要么与我调用 API 的方式有关,要么与我的函数调用有关,可能会在整个运行时将第一个 API 响应存储在它们内部并继续使用它,即使是新的 API 已获取响应。

另外 URL 我构造的似乎是正确的,当我将它们粘贴到我的浏览器时给我正确的 JSON。 API 响应 JSON 中我想要的数据在“评论”数组中。每个元素都有一个“评论”键,其值包含实际用户评论。

import requests
import string

REVIEWS_COUNT = 500
REVIEWS_PER_REQUEST = 20

game_IDs = {
    "AAA": {
        "KINGDOM_COME_DELIVERANCE": "379430",
        "ASSASSINS_CREED_ODYSSEY": "812140",
    },

    "indie": {
        "RIMWORLD": "294100",
        "DUSK": "519860",
    }
}

def review_to_words(review):
    """
    input : a string "review"
    filters out anything else than pure words
    output: a string list of words in that review
    """
    words = []
    review.translate(str.maketrans('', '', string.punctuation))
    review.replace('\n', '')
    review.replace('☐', '', 1)
    review.lower()
    for word in review.split(' '):
        words.append(word)
    return words

def response_to_words(response):
    """
    input : the Steam API response
    output: a string list of words in user reviews of the Steam API response
    """
    all_words = []
    jres = response.json()
    jarr = jres['reviews']
    for jelem in jarr:
        rev = jelem['review']
        for word in review_to_words(rev):
            all_words.append(word)
    return all_words

def main():
    for game_type in game_IDs:
        for game_name in game_IDs[game_type]:
            game_id = game_IDs[game_type][game_name]
            words_count = {}  # dictionary: word -> count
            for revs_idx in range(0, REVIEWS_COUNT, REVIEWS_PER_REQUEST):
                api_url = "https://store.steampowered.com/appreviews/" + game_id + "?json=1&start_offset=" + str(revs_idx)
                response = requests.get(api_url)
                words = response_to_words(response)
                for word in words:
                    if word not in words_count:
                        words_count[word] = 0
                    words_count[word] += 1
            with open('03_words_count.txt', 'w', encoding='utf-8') as f:
                f.write(str(words_count))

if __name__ == '__main__':
    main()

Steam 已更改为不再使用 start_offset。他们现在使用游标,其工作方式不同。
Getting all reviews from a steam game using Steamworks?
https://partner.steamgames.com/doc/store/getreviews