从steam id获取蒸汽游戏

时间:2017-11-07 19:01:42

标签: python python-3.6 steam

我正在尝试创建一个获取steam id的python程序,并返回特定用户在其Library中的游戏。我在网上搜索,没有发现任何蟒蛇。你能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

我找到了这个开发者网站的Steam,你可以在python实现中使用它。

https://developer.valvesoftware.com/wiki/SteamID

  

Steam ID作为Steam社区ID

     

可以将Steam ID转换为Steam社区ID,以便在Steam社区网站上使用。   设X,Y和Z常数由SteamID定义:STEAM_X:Y:Z。   有两种转换方法:

     

适用于32位系统

     

使用公式W=Z*2+Y,可以将SteamID转换为以下链接:      http或https://steamcommunity.com/path/[letter:1:W]      帐户类型字母可在上表中找到。路径可以在斜杠符号后面的相同位置找到。      示例:http://steamcommunity.com/gid/[g:1:4]

     

对于64位系统:

     

设V为帐户类型的SteamID64标识符(可在上表中以十六进制格式找到)。      使用公式W=Z*2+V+Y,可以将SteamID转换为以下链接:      http或https://steamcommunity.com/path/W      对于32位方法,可以在上面的表中找到路径,再次在斜杠之后。      示例:http://steamcommunity.com/profiles/76561197960287930

答案 1 :(得分:0)

试试这个(我用完全错误处理为你写了一个类,你可以用BeautifulSoup或其他包编写这个代码更高效...(对私人用户不起作用!)):

import requests, json


class SteamGameGrabber(object):

    def __init__(self):

        self.url = ''
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
        self.start_tag = "var rgGames"
        self.end_tag = "var rgChangingGames"

        self.last_data = {}

    def send_request(self):

        try:
            response = requests.get(self.url, headers=self.headers)
            raw_data = str(response.content, encoding="utf-8")
        except:
            return [False, "Something Wrong! Connection Lost!"]

        else:
            if response.status_code == 200:
                return [True, raw_data]
            else:
                return [False, "Check Your Connection ( status_code is Not 200 )!"]

    def pars_data(self, get_input):

        def find_between(s, first, last):

            try:
                start = s.index(first) + len(first)
                end = s.index(last, start)
            except:
                return [False, "Parsing Error"]
            else:
                return [True, s[start:end]]

        if "<title>Steam Community :: Error</title>" in get_input:
            return [False, "I Can Not Find This ID on Steam Server"]

        if '<div class="profile_private_info">' in get_input:
            return [False, "This profile is private, I can not Decode it, sorry."]

        get_data = find_between(get_input, self.start_tag, self.end_tag)

        if get_data[0] is True:

            dict_data = json.loads(get_data[1].strip().lstrip("=").rstrip(";").strip())

            try:
                for box in dict_data:

                    game_id = str(box['appid']).strip()
                    game_name = box['name'].strip()

                    if game_name in self.last_data:
                        pass

                    else:

                        self.last_data[game_name] = game_id
            except:
                return [False, "Format is Wrong"]

            else:
                return [True, self.last_data]

        else:
            return [False, get_data[1]]

    def call_all(self, get_id):

        if get_id.strip() == "":
            return "Please Insert Your Steam ID"

        else:

            self.url = 'http://steamcommunity.com/id/{0}/games/?tab=all'.format(get_id)

            get_state_1 = self.send_request()

            if get_state_1[0] is True:

                get_state_2 = self.pars_data(get_state_1[1])
                return get_state_2[1]

            else:
                return get_state_1[1]



# Call This Class Now:

your_id = 'erfan1'

make_object = SteamGameGrabber()
get_result = make_object.call_all(your_id)

if isinstance(get_result, dict):

    print("Dict Format : ", get_result, "\n")
    print("Target ID : ", my_id, "\n")

    for names, appid in get_result.items():
        print(names, appid)

else:
    print(get_result)

祝你好运...... :)