我使用 steam api 和 python 来获取玩 Dota 2 等游戏的玩家数量。
import requests
import numpy as np
import pandas as pd
def main():
header = {"Client-ID": "F07D7ED5C43A695B3EBB01C28B6A18E5"}
appId = 570
game_players_url = 'https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?format=json&appid=' + appId
game_players = requests.get(game_players_url, headers=header)
print("Game name: Dota 2" + ", Player count: " + str(game_players.json()['response']['player_count']))
if __name__ == '__main__':
main()
这为我提供了特定游戏(在本例中为 Dota 2)的正确“当前”玩家数量,但是我需要的是有关该特定游戏的玩家数量的历史数据。 这应该是可能的,因为this网站有我想要的信息,并且他们可能从 Steam API 获取数据。 任何帮助将不胜感激!
谢谢你
import aiohttp
import http
from bs4 import BeautifulSoup
async def playercount(gameid):
url = f'https://steamcharts.com/app/{gameid}'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as response:
if response.status != 200:
return{"error": {"code": response.status, "message": http.HTTPStatus(response.status).phrase}}
html = await response.text()
soup = BeautifulSoup(html, 'html.parser')
data = {}
count = 0
for stats in soup.find_all('div', class_='app-stat'):
soup2 = BeautifulSoup(str(stats), 'html.parser')
for stat in soup2.find_all('span', class_='num'):
stat = str(stat).replace('<span class="num">', '').replace('</span>', '')
if count == 0:
data['Current Players'] = stat
elif count == 1:
data['Peak Players 24h'] = stat
elif count == 2:
data['Peak Players All Time'] = stat
count += 1
return data