Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python从json api获取特定值_Python_Json_Api_Bots - Fatal编程技术网

如何使用python从json api获取特定值

如何使用python从json api获取特定值,python,json,api,bots,Python,Json,Api,Bots,所以我有从一个api获取数据的代码,但我只能使用前3个键,比如message idk,为什么代码是: import requests import json result=requests.get('https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2&page=1&

所以我有从一个api获取数据的代码,但我只能使用前3个键,比如message idk,为什么代码是:

   import requests
    import json
 
    result=requests.get('https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2&page=1&offset=1&sort=asc&apikey=mytoken')
    result.status_code
    result.text
    result.json()
    
    print (result.json()['message]) # work
    print (result.json()['gas]) # or any other key dont work
api的输出:

{"status":"1","message":"OK","result":[{"blockNumber":"4620855","timeStamp":"1511634257","hash":"0x5c9b0f9c6c32d2690771169ec62dd648fef7bce3d45fe8a6505d99fdcbade27a","nonce":"5417","blockHash":"0xee385ac028bb7d8863d70afa02d63181894e0b2d51b99c0c525ef24538c44c24","from":"0x731c6f8c754fa404cfcc2ed8035ef79262f65702","contractAddress":"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2","to":"0x642ae78fafbb8032da552d619ad43f1d81e4dd7c","value":"1000000000000000000000000","tokenName":"Maker","tokenSymbol":"MKR","tokenDecimal":"18","transactionIndex":"55","gas":"3000000","gasPrice":"1000000000","gasUsed":"1594668","cumulativeGasUsed":"4047394","input":"deprecated","confirmations":"7045304"}]}
我只能获取状态消息ect。 当我尝试使用gas时,这是错误的

回溯(最近一次呼叫最后一次): 文件“main.py”,第11行,在 打印(result.json()[gas])
NameError:未定义名称“gas”

请仔细查看JSON响应。它是一个有三个键(状态、消息和结果)的字典。关键结果是列表中有另一个字典。 JSON响应:

{
   "status":"1",
   "message":"OK",
   "result":[
      {
         "blockNumber":"4620855",
         "timeStamp":"1511634257",
         "hash":"0x5c9b0f9c6c32d2690771169ec62dd648fef7bce3d45fe8a6505d99fdcbade27a",
         "nonce":"5417",
         "blockHash":"0xee385ac028bb7d8863d70afa02d63181894e0b2d51b99c0c525ef24538c44c24",
         "from":"0x731c6f8c754fa404cfcc2ed8035ef79262f65702",
         "contractAddress":"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2",
         "to":"0x642ae78fafbb8032da552d619ad43f1d81e4dd7c",
         "value":"1000000000000000000000000",
         "tokenName":"Maker",
         "tokenSymbol":"MKR",
         "tokenDecimal":"18",
         "transactionIndex":"55",
         "gas":"3000000",
         "gasPrice":"1000000000",
         "gasUsed":"1594668",
         "cumulativeGasUsed":"4047394",
         "input":"deprecated",
         "confirmations":"7045304"
      }
   ]
}
def price_of_gas(inp):
    def recursive_function(inp):
        if type(inp) is list:
            for i in inp:
                ans = recursive_function(i)
                if ans!=None: return ans
        elif type(inp) is dict:
            if 'gas' in inp: return inp['gas']
            for i in inp:
                ans = recursive_function(inp[i])
                if ans!=None: return ans
        else: return None
    ans = recursive_function(inp)
    return ans if ans else "Could NOT find the gas"
price_of_gas(your_reponse)
因此,如果需要访问结果字典中的键,则必须相应地嵌套代码

result.json['result'][0]['gas']

您应该添加一些打印语句来理解您的响应和调试

your_reponse = {
    'message': 'OK',
    'result': [{'blockHash': '0xee385ac028bb7d8863d70afa02d63181894e0b2d51b99c0c525ef24538c44c24',
    'blockNumber': '4620855',
    'confirmations': '7045304',
    'contractAddress': '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2',
    'cumulativeGasUsed': '4047394',
    'from': '0x731c6f8c754fa404cfcc2ed8035ef79262f65702',
    'gas': '3000000',
    'gasPrice': '1000000000',
    'gasUsed': '1594668',
    'hash': '0x5c9b0f9c6c32d2690771169ec62dd648fef7bce3d45fe8a6505d99fdcbade27a',
    'input': 'deprecated',
    'nonce': '5417',
    'timeStamp': '1511634257',
    'to': '0x642ae78fafbb8032da552d619ad43f1d81e4dd7c',
    'tokenDecimal': '18',
    'tokenName': 'Maker',
    'tokenSymbol': 'MKR',
    'transactionIndex': '55',
    'value': '1000000000000000000000000'}],
    'status': '1'}
使用此递归函数解决API响应中的更改:

{
   "status":"1",
   "message":"OK",
   "result":[
      {
         "blockNumber":"4620855",
         "timeStamp":"1511634257",
         "hash":"0x5c9b0f9c6c32d2690771169ec62dd648fef7bce3d45fe8a6505d99fdcbade27a",
         "nonce":"5417",
         "blockHash":"0xee385ac028bb7d8863d70afa02d63181894e0b2d51b99c0c525ef24538c44c24",
         "from":"0x731c6f8c754fa404cfcc2ed8035ef79262f65702",
         "contractAddress":"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2",
         "to":"0x642ae78fafbb8032da552d619ad43f1d81e4dd7c",
         "value":"1000000000000000000000000",
         "tokenName":"Maker",
         "tokenSymbol":"MKR",
         "tokenDecimal":"18",
         "transactionIndex":"55",
         "gas":"3000000",
         "gasPrice":"1000000000",
         "gasUsed":"1594668",
         "cumulativeGasUsed":"4047394",
         "input":"deprecated",
         "confirmations":"7045304"
      }
   ]
}
def price_of_gas(inp):
    def recursive_function(inp):
        if type(inp) is list:
            for i in inp:
                ans = recursive_function(i)
                if ans!=None: return ans
        elif type(inp) is dict:
            if 'gas' in inp: return inp['gas']
            for i in inp:
                ans = recursive_function(inp[i])
                if ans!=None: return ans
        else: return None
    ans = recursive_function(inp)
    return ans if ans else "Could NOT find the gas"
price_of_gas(your_reponse)

非常感谢,伙计,我还有一个问题,机器人是否有机会检查api更改,以便api更改机器人打印气体,以便我可以监控以太网络上的气体谢谢:)是的,创建一个递归函数,查找密钥
气体
谢谢,我会刹车我的头我会让它工作,但如何运行它每30秒,并把它发送给迪斯科我工作了5个小时,我不能找出它,你能帮我吗
def price_of_gas(inp):
    def recursive_function(inp):
        if type(inp) is list:
            for i in inp:
                ans = recursive_function(i)
                if ans!=None: return ans
        elif type(inp) is dict:
            if 'gas' in inp: return inp['gas']
            for i in inp:
                ans = recursive_function(inp[i])
                if ans!=None: return ans
        else: return None
    ans = recursive_function(inp)
    return ans if ans else "Could NOT find the gas"
price_of_gas(your_reponse)