【问题标题】:How to get specific value from json api with python如何使用python从json api获取特定值
【发布时间】:2021-04-21 06:52:14
【问题描述】:

所以我有从一个 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"}]}

我只能获取状态消息等。 当我尝试加油时,这是错误

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

【问题讨论】:

    标签: python json api bots


    【解决方案1】:

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

    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'}
    
    >>> your_reponse['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'}]
    
    >>> print(your_reponse['result'][0]['gas'])
    
    3000000
    

    使用这个递归函数来解决 API 响应的变化:

    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 更改机器人将打印气体,以便我可以监控以太网络上的气体,谢谢 :)
    • 是的,创建一个递归函数来查找密钥gas
    • 我正在制动我的头 id 让它工作,但如何每​​ 30 秒运行一次并将它发送到不和谐我正在工作 5 小时我无法弄清楚你能帮我吗
    【解决方案2】:

    仔细查看 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"
          }
       ]
    }
    

    因此,如果您需要访问结果字典中存在的键,则必须相应地嵌套您的代码。

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

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 1970-01-01
      • 2021-12-14
      • 2021-12-19
      • 2021-12-20
      • 1970-01-01
      • 2022-01-09
      • 2017-09-19
      • 1970-01-01
      相关资源
      最近更新 更多