python - Steam API 获取价格列表

标签 python

我试图获取一份价格表。到目前为止,我的代码是:

def steamlibrarypull(steamID, key):
    #Pulls out a CSV of Steam appids.
    steaminfo = {
        'key': key,
        'steamid': steamID,
        'format':'JSON',
        'include_appinfo':'1'
    }
    r = requests.get('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/', params=steaminfo)
    d = json.loads(r.content)
    I = d['response']['games']
    B = {}
    for games in I:
        B[games['name'].encode('utf8')] = games['appid']
    with open('games.csv', 'w') as f:
        for key, value in B.items():
            f.write("%s,%s\r\n" % (key, value))
    return B

但是我希望能够执行 request.get 来获取这本字典并输出价格列表。 https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI似乎需要 CSV 列表,但这真的有必要吗?

最佳答案

这是一个非正式的 Steam API,意味着 Steam 会根据他们认为合适的方式进行修改。目前它不支持多个appid,如所述 here .
用它来获取您想要玩的游戏的价格

http://store.steampowered.com/api/appdetails/?appids=237110&cc=us&filters=price_overview

根据上面的代码,您将需要知道如何迭代字典并在取回后更新商店价格。

def steamlibrarypull(steamID, key):
#Pulls out a CSV of Steam appids.
    steaminfo = {
        'key': key,
        'steamid': steamID,
        'format':'JSON',
        'include_appinfo':'1'
    }
    r = requests.get('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/', params=steaminfo)
    d = json.loads(r.content)
    response = d['response']['games']
    games = {}
    for game in response:
        getprice = requests.get('http://store.steampowered.com/api/appdetails/?appids=%d&filters=price_overview&cc=us' % game['appid'])
        if getprice.status_code == 200:
            rjson = json.loads(getprice.text)
            # use the appid to fetch the value and convert to decimal
            # appid is numeric, cast to string to lookup the price
            try:
                price = rjson[str(game['appid'])]['data']['price_overview']['initial'] * .01
            except:
                price = 0
            games[game['name']] = {'price': price, 'appid': game['appid']}

这将返回以下字典:

{u'Half-Life 2: Episode Two': {'price': 7.99, 'appid': 420}

通过 appid 而不是名称进行导航会更容易,但根据您的请求和原始结构,应该这样做。然后,这将为您提供名称、appid 和价格,您可以进一步使用它们或将其写入文件。 请注意,这不包括 sleep 定时器,如果你的游戏列表很长,你应该在进行另一个游戏之前让你的 api 调用休眠 2 秒,否则 api 会阻止你并且不会返回数据,这将在 python 中导致错误解析价格。

关于python - Steam API 获取价格列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29591313/

相关文章:

python - Pandas Groupby Agg 函数中的列顺序

python - 地址清理 Boost.Python 模块

python - 更新指向包含空格的保管箱目录的 PYTHONPATH 变量

java - 无法使用 Jython 和 mysql.connector 模块连接到 mysql 数据库

python - Python中根据特定顺序排列括号中的数据

python - 这是 Python 正则表达式中的错误吗?

python - 如何在 Python 中进行导入

python - Django:从数据库显示的 DecimalField 格式值

python - 禁止用户并将其添加到表中时出现问题

python - 如何忽略 ‘imported but unused’ 文件中的 Pyflakes 错误 ‘__init__.py’?