我是python新手。我尝试从api中读取一些数据,但在第二个url中出现问题,我不知道当我有更多数据时如何解析api。第一个URL只有一个“名称”,第二个URL有更多名称。。。。 如何打印第二个url中的所有名称。。。 谢谢你
import os
from urllib import urlopen
import json
url = urlopen('https://www.coincalculators.io/api.aspx?name=ethereum&hashrate=420000000&power=0&poolfee=1&powercost=0&difficultytime=0').read()
data= json.loads(url)
name = data["name"] # OK
print name
rewardsInMonth = data["rewardsInMonth"] # OK
print rewardsInMonth
#url with more names
url2 = urlopen('https://www.coincalculators.io/api/allcoins.aspx?hashrate=420000000&power=0&powercost=0&difficultytime=0&algorithm=Ethash').read()
data2= json.loads(url2)
name2 = data2["name"] # OK
print name2
我看到有一个方括号在第二个网址,这里是我的问题
这个API的不同端点返回不同的数据结构。第一个调用只返回一个dict,第二个调用(到
allcoins.aspx
端点)毫不奇怪地返回dict列表非常有礼貌地将您推到python3和
requests
模块的方向,第二个URL返回一个列表,因此您可以使用for
循环来迭代硬币:相关问题 更多 >
编程相关推荐