首页 > 解决方案 > 解析 urllib 请求

问题描述

我从代码中有以下巨大的输出:urllib.request.urlopen("https://api...").read()

这看起来像一个 JSON 对象,但它是一个字节对象。我正在研究整个数据。我不确定如何解析所有这些嵌套字典。任何帮助,将不胜感激。我想提取位于末尾的值 112242287903649 。

b'{"address":"0x4264422fa4c1e60c2ee10d19549c0775fe544d7c","ETH":{"balance":39234.92760140797,"price":{"rate":406.0918669863694,"diff":3.33,"diff7d":7.19,"ts":1603860182,"marketCapUsd":45964513524.05101,"availableSupply":113187476.1865,"volume24h":14765115042.093159,"diff30d":14.028844201369225}},"countTxs":7,"tokens":[{"tokenInfo":{"address":"0x0d4b4da5fb1a7d55e85f8e22f728701ceb6e44c9","name":"DigiMax","decimals":"18","symbol":"DGMT","totalSupply":"1000000000000000000000000000","owner":"0x","lastUpdated":1603831313,"issuancesCount":0,"holdersCount":1042,"description":"DigiMax (DGMT) is a de-centralized Currency on ETHEREUM NETWORK. It is trustless, non-custodial, Layer-2 scaling solution for transferring value on Ethereum. It is Open Source. Community oriented and powered to maximize the power of the blockchain technology","website":"https://digimaxtoken.io/","twitter":"DigiMax_DGMT","image":"/images/DGMT0d4b4da5.png","telegram":"https://t.me/DigiMaxToken","reddit":"DigiMax_DGMT","coingecko":"digimax","price":{"rate":1.218303675e-5,"diff":3.55,"diff7d":-87.33,"ts":1603860187,"marketCapUsd":0,"availableSupply":0,"volume24h":0.36549128,"diff30d":-99.95948266499424,"currency":"USD"}},"balance":3.9e+19,"totalIn":0,"totalOut":0},{"tokenInfo":{"address":"0x28cb7e841ee97947a86b06fa4090c8451f64c0be","name":"YF Link","decimals":"18","symbol":"YFL","totalSupply":"52000000000000000000000","owner":"0x","lastUpdated":1603851830,"issuancesCount":0,"holdersCount":5164,"image":"/images/YFL28cb7e84.png","website":"https://yflink.io/","telegram":"https://t.me/YFLinkGroup","twitter":"YFLinkio","coingecko":"yflink","price":{"rate":411.62315709142763,"diff":2.44,"diff7d":22.67,"ts":1603860243,"marketCapUsd":20628385.985420085,"availableSupply":50114.73633112,"volume24h":673808.77973096,"diff30d":-9.745291974110742,"currency":"USD"},"publicTags":["Yield Farming","Yearn","Governance"]},"balance":69000000000000,"totalIn":0,"totalOut":0},{"tokenInfo":{"address":"0x618e75ac90b12c6049ba3b27f5d5f8651b0037f6","name":"QASH","decimals":"6","symbol":"QASH","totalSupply":"1000000000000000","owner":"0x9fa8a9cd0bd7cbfc503513bc94cd3b3a9ca90e35","lastUpdated":1603818056,"issuancesCount":0,"holdersCount":13087,"website":"https://liquid.plus/","facebook":"LiquidGlobal","telegram":"https://t.me/QUOINENews","twitter":"Liquid_Global","image":"/images/QASH618e75ac.jpeg","reddit":"liquid","coingecko":"qash","ethTransfersCount":2,"price":{"rate":0.03783789848158,"diff":2.83,"diff7d":0.05,"ts":1603860243,"marketCapUsd":13243264.468553,"availableSupply":350000000,"volume24h":170565.95092274,"diff30d":-5.421371004476654,"currency":"USD"},"publicTags":["Exchange"]},"balance":112242287903649,"totalIn":0,"totalOut":0},{"tokenInfo":{"address":"0x9f7229af0c4b9740e207ea283b9094983f78ba04","decimals":"18","name":"Tadpole","owner":"0x","symbol":"TAD","totalSupply":"1000000000000000000000000","lastUpdated":1603859098,"issuancesCount":0,"holdersCount":597,"price":false},"balance":100000000000000,"totalIn":0,"totalOut":0}]}'

标签: pythonurllib

解决方案


内置json模块完全能够解析字节字符串-

import json
response = urllib.request.urlopen("https://api/endpoint").read()
jsondat = json.loads(response)

现在,您可以随意使用jsondat并提取所需的任何嵌套属性。

请注意,您也可以使用该requests模块,尽管在这种情况下您绝对不必这样做,以更简单地实现这一点 -

import requests

jsondat = requests.get("https://api/endpoint").json()

json.loads给定的字节字符串进行操作会产生-

{'address': '0x4264422fa4c1e60c2ee10d19549c0775fe544d7c',
 'ETH': {'balance': 39234.92760140797,
  'price': {'rate': 406.0918669863694,
   'diff': 3.33,
   'diff7d': 7.19,
   'ts': 1603860182,
   'marketCapUsd': 45964513524.05101,
   'availableSupply': 113187476.1865,
   'volume24h': 14765115042.093159,
   'diff30d': 14.028844201369225}},
 'countTxs': 7,
 'tokens': [{'tokenInfo': {'address': '0x0d4b4da5fb1a7d55e85f8e22f728701ceb6e44c9',
    'name': 'DigiMax',
    'decimals': '18',
    'symbol': 'DGMT',
    'totalSupply': '1000000000000000000000000000',
    'owner': '0x',
    'lastUpdated': 1603831313,
    'issuancesCount': 0,
    'holdersCount': 1042,
    'description': 'DigiMax (DGMT) is a de-centralized Currency on ETHEREUM NETWORK. It is trustless, non-custodial, Layer-2 scaling solution for transferring value on Ethereum. It is Open Source. Community oriented and powered to maximize the power of the blockchain technology',
    'website': 'https://digimaxtoken.io/',
    'twitter': 'DigiMax_DGMT',
    'image': '/images/DGMT0d4b4da5.png',
    'telegram': 'https://t.me/DigiMaxToken',
    'reddit': 'DigiMax_DGMT',
    'coingecko': 'digimax',
    'price': {'rate': 1.218303675e-05,
     'diff': 3.55,
     'diff7d': -87.33,
     'ts': 1603860187,
     'marketCapUsd': 0,
     'availableSupply': 0,
     'volume24h': 0.36549128,
     'diff30d': -99.95948266499424,
     'currency': 'USD'}},
   'balance': 3.9e+19,
   'totalIn': 0,
   'totalOut': 0},
  {'tokenInfo': {'address': '0x28cb7e841ee97947a86b06fa4090c8451f64c0be',
    'name': 'YF Link',
    'decimals': '18',
    'symbol': 'YFL',
    'totalSupply': '52000000000000000000000',
    'owner': '0x',
    'lastUpdated': 1603851830,
    'issuancesCount': 0,
    'holdersCount': 5164,
    'image': '/images/YFL28cb7e84.png',
    'website': 'https://yflink.io/',
    'telegram': 'https://t.me/YFLinkGroup',
    'twitter': 'YFLinkio',
    'coingecko': 'yflink',
    'price': {'rate': 411.62315709142763,
     'diff': 2.44,
     'diff7d': 22.67,
     'ts': 1603860243,
     'marketCapUsd': 20628385.985420085,
     'availableSupply': 50114.73633112,
     'volume24h': 673808.77973096,
     'diff30d': -9.745291974110742,
     'currency': 'USD'},
    'publicTags': ['Yield Farming', 'Yearn', 'Governance']},
   'balance': 69000000000000,
   'totalIn': 0,
   'totalOut': 0},
  {'tokenInfo': {'address': '0x618e75ac90b12c6049ba3b27f5d5f8651b0037f6',
    'name': 'QASH',
    'decimals': '6',
    'symbol': 'QASH',
    'totalSupply': '1000000000000000',
    'owner': '0x9fa8a9cd0bd7cbfc503513bc94cd3b3a9ca90e35',
    'lastUpdated': 1603818056,
    'issuancesCount': 0,
    'holdersCount': 13087,
    'website': 'https://liquid.plus/',
    'facebook': 'LiquidGlobal',
    'telegram': 'https://t.me/QUOINENews',
    'twitter': 'Liquid_Global',
    'image': '/images/QASH618e75ac.jpeg',
    'reddit': 'liquid',
    'coingecko': 'qash',
    'ethTransfersCount': 2,
    'price': {'rate': 0.03783789848158,
     'diff': 2.83,
     'diff7d': 0.05,
     'ts': 1603860243,
     'marketCapUsd': 13243264.468553,
     'availableSupply': 350000000,
     'volume24h': 170565.95092274,
     'diff30d': -5.421371004476654,
     'currency': 'USD'},
    'publicTags': ['Exchange']},
   'balance': 112242287903649,
   'totalIn': 0,
   'totalOut': 0},
  {'tokenInfo': {'address': '0x9f7229af0c4b9740e207ea283b9094983f78ba04',
    'decimals': '18',
    'name': 'Tadpole',
    'owner': '0x',
    'symbol': 'TAD',
    'totalSupply': '1000000000000000000000000',
    'lastUpdated': 1603859098,
    'issuancesCount': 0,
    'holdersCount': 597,
    'price': False},
   'balance': 100000000000000,
   'totalIn': 0,
   'totalOut': 0}]}

推荐阅读