-2

I'm trying to extract a value called "balanceStr" from a web scraper I'm making, but I'm not having much luck!

Here's where I got so far:

import requests
import json

url = "https://www.hpbscan.org/HpbScan/addrs/getAddressDetailInfo"
headers = {'Content-Type': 'application/json;charset=utf-8'}
body = '["0x7EC332476fCA4Bcd20176eE06F16960b5D49333e"]'

data = requests.post(url, data=body, headers=headers)

json_data = (json.loads(data.text))
#print(json_data)
print(json_data['balanceStr'])

If I just print(json_data) this is the output, but I'm just trying to get the value of balanceStr (36885.403...) in a variable so that I can use it like print(currentbalance)

[
    "000000",
    "\u6210\u529f",
    {
        "addrs": {
            "accountType": null,
            "address": "0x7ec332476fca4bcd20176ee06f16960b5d49333e",
            "assetRankingOrder": "150",
            "balance": 36885403823844342504238,
            "balanceStr": "36885.40382384",
            "createTimestamp": 1569209857000,
            "fromCount": 22,
            "lastestBlock": 3951440,
            "map": {},
            "number": 229,
            "percentRate": "0.0369%",
            "startBlock": 51601,
            "toCount": 15
        },
        "hpbInstantPrice": {
            "changePercent": "-6.65%",
            "cnyPrice": "1.9890",
            "id": 1,
            "map": {},
            "updateTime": 1569210840000,
            "usdPrice": "0.2791"
        },
        "nonce": 22
    }
]

When I print(json_data['balanceStr']) this is the error I'm getting:

    print(json_data['addrs'])
TypeError: list indices must be integers or slices, not str

Any help would be much appreciated!

5 Answers5

0

json_data contains a list of objects. You can use

json_data[2]['addrs']

to read the third object, which seems to be the object you're trying to access.

Then

json_data[2]['addrs']["balanceStr"]

to read the final value you're after.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
0

As the error message says, json_data is a list. The item you want is in json_data[2]. But the particular part you want, balanceStr, is inside an element named addrs. Putting this all together, you get:

json_data[2]['addrs']["balanceStr"]
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

It looks like your json's lowest level is a list, this means to get the dict object needed to get 'balanceStr'. in this case its json_data[2]['addrs']["balanceStr"].

json_data[2]['addrs']["balanceStr"]
Equilibris
  • 49
  • 1
  • 4
0

Here

import requests

url = "https://www.hpbscan.org/HpbScan/addrs/getAddressDetailInfo"
headers = {'Content-Type': 'application/json;charset=utf-8'}
body = '["0x7EC332476fCA4Bcd20176eE06F16960b5D49333e"]'

data = requests.post(url, data=body, headers=headers).json()

balance_str = data[2]['addrs']['balanceStr']
print(balance_str)

output

36886.89003793
balderman
  • 22,927
  • 7
  • 34
  • 52
  • Thanks for your help, I tried your version but I don't get the same output sadly: balance_str = data[2]['addrs']['balanceStr'] TypeError: 'Response' object is not subscriptable But you've helped me find the solution I needed with [2]['addrs']['balanceStr']! – lieutenantprivatesprite Sep 23 '19 at 12:55
  • `data = requests.post(url, data=body, headers=headers).json()` see this line in the code. Make sure you have it – balderman Sep 23 '19 at 13:03
0

Treat the json output as a dictionary/list that may contain nested dictionaries or lists, you should adress each variable accordingly.

Try with:

json_data[2]['addrs']['balancestr']

Output:

36885.40382384

json_data in this case must be adressed as a list that contains nested diciontaries. You need to adress the position in which addrs is. Also check this answer as this is a duplicate. How to obtain info from json

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53