从互联网上提取JSON数据并用Python打印?

2024-06-16 12:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python开发一个Twitch聊天机器人。然而,我有一些麻烦的功能,已经要求了很多。我需要从JSON文件中提取“gameserverid”和“gameextrainfo”数据。example file

import urllib2
import json
req = urllib2.Request("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=605C90955CFDE6B1CB7D2EFF5FE824A0&steamids=76561198022404556")
opener = urllib2.build_opener()
f = opener.open(req)
json = json.loads(f.read())
currentlyPlaying = json[gameextrainfo]
gameServer = json[gameserverid]

这是我目前掌握的密码。我想得到它,以便其他命令可以打印变量“currentlyPlaying”和“gameServer”到IRC聊天。但是,当我这样做时,我会在控制台中看到:

Traceback (most recent call last):
  File "N:/_DEVELOPMENT/Atlassian Cloud/TwitchChatBot/Testing/grabplayerinfofromsteam.py", line 1, in <module>
    import urllib2
ImportError: No module named 'urllib2'

有什么想法吗?我在Windows环境中,运行最新版本的Python(python3.5.1)


Tags: 文件import功能json机器人openerurllib2req