站点 API 允许您接收 json 格式的数据:
{
"query": {
"count": 2
},
" results": {
"EUR_RUB": {
"id": "EUR_RUB",
"val": 75.489179,
"to": "RUB",
"fr": "EUR"
},
"USD_RUB": {
"id": "USD_RUB",
"val": 66.343801,
"to": "RUB",
"fr": "USD"
}
}
}
我正在尝试从中获取数据:
import json
from urllib.request import urlopen
with urlopen('https://free.currencyconverterapi.com/api/v6/convert?q=EUR_RUB,USD_RUB') as response:
source = response.read()
data = json.loads(source)
for item in data["results"]:
print(item)
但是,在编写完脚本后,我只得到:
EUR_RUB
USD_RUB
请告诉我如何正确编写请求。
使用请求模块的示例:
结果:
通过这样的循环,只遍历字典的键。如果要遍历所有key:value 对,请使用:
如果只有值: