如何使用 requests.get 访问文本格式为 JSON 的 URL,另存为字典
How to use requests.get to access a URL with text formatted as a JSON, save as a dictionary
我正在尝试使用请求访问 URL,文本格式为 JSON。在下面的代码中,一切正常,但我希望它是一个字典,但是我请求的响应类型是:'class method'(我第一次打印了错误对象的类型)。我怎样才能把信息变成字典?
import requests
import json
r=requests.get('https://s3-us-west-1.amazonaws.com/riot-api/seed_data/matches1.json')
match_histories = r.json()
print(type(match_histories))
代码永远不会超过行
match_histories = r.json()
它只是说它 运行(它不会冻结)。
但是,如果我下载文件并尝试它,它会起作用(不过我希望它直接从网站下载)
with open('matches1', 'r') as json_file:
match_histories = json.load(json_file)
match_histories['matches']
产生了我所希望的。
我在 JSONs here.
上阅读了有关使用请求的信息
请注意该文件非常大,并且采用适合字典的格式。
我要加载的文件可以是downloaded但是结构基本是:
{
"matches": [
{
"matchId": 1778839570,
"region": "NA",
"platformId": "NA1",
"matchMode": "CLASSIC",
"matchType": "MATCHED_GAME",
"matchCreation": 1427867835805,
"matchDuration": 3424,
"queueType": "RANKED_SOLO_5x5",
"mapId": 11,
"season": "SEASON2015",
"matchVersion": "5.6.0.194",
"participants": [
// more dictionaries
],
"participantIdentities": [
// more dictionaries
],
"teams": [
// more dictionaries
],
"timeline": {
"frames": [
// many frame dictionaries
],
"frameInterval": 60000
}
},
// more dictionaries
]
}
答案可能很简单。抱歉,我是 Python/programming.
的新手
如何将 URL 中的信息保存为字典?
它有效,花了很长时间。为什么步骤
match_histories=r.json()
比
花费的时间长得多
match_histories =json.load(json_file)
?
您需要调用响应对象的方法.json()
:
r = requests.get('https://s3-us-west-1.amazonaws.com/riot-api/seed_data/matches1.json')
match_histories = r.json()
我正在尝试使用请求访问 URL,文本格式为 JSON。在下面的代码中,一切正常,但我希望它是一个字典,但是我请求的响应类型是:'class method'(我第一次打印了错误对象的类型)。我怎样才能把信息变成字典?
import requests
import json
r=requests.get('https://s3-us-west-1.amazonaws.com/riot-api/seed_data/matches1.json')
match_histories = r.json()
print(type(match_histories))
代码永远不会超过行
match_histories = r.json()
它只是说它 运行(它不会冻结)。
但是,如果我下载文件并尝试它,它会起作用(不过我希望它直接从网站下载)
with open('matches1', 'r') as json_file:
match_histories = json.load(json_file)
match_histories['matches']
产生了我所希望的。
我在 JSONs here.
上阅读了有关使用请求的信息请注意该文件非常大,并且采用适合字典的格式。
我要加载的文件可以是downloaded但是结构基本是:
{
"matches": [
{
"matchId": 1778839570,
"region": "NA",
"platformId": "NA1",
"matchMode": "CLASSIC",
"matchType": "MATCHED_GAME",
"matchCreation": 1427867835805,
"matchDuration": 3424,
"queueType": "RANKED_SOLO_5x5",
"mapId": 11,
"season": "SEASON2015",
"matchVersion": "5.6.0.194",
"participants": [
// more dictionaries
],
"participantIdentities": [
// more dictionaries
],
"teams": [
// more dictionaries
],
"timeline": {
"frames": [
// many frame dictionaries
],
"frameInterval": 60000
}
},
// more dictionaries
]
}
答案可能很简单。抱歉,我是 Python/programming.
的新手如何将 URL 中的信息保存为字典?
它有效,花了很长时间。为什么步骤
match_histories=r.json()
比
花费的时间长得多match_histories =json.load(json_file)
?
您需要调用响应对象的方法.json()
:
r = requests.get('https://s3-us-west-1.amazonaws.com/riot-api/seed_data/matches1.json')
match_histories = r.json()