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

时间:2016-05-30 19:33:17

标签: python json

我正在用Python开发一个Twitch聊天机器人。但是,我在使用已经请求了很多功能方面遇到了一些问题。我需要拉动" gameserverid"和#34; gameextrainfo"来自JSON文件的数据。 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"和#34; 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(Python 3.5.1)

1 个答案:

答案 0 :(得分:2)

presence_of_element_located

但不要使用try: import urllib.request as urllib2 except ImportError: import urllib2 ,请使用请求!

urllib2

http://docs.python-requests.org/en/master/