Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Value Of API Dictionary

I’m using a service with an API key and I want to print my balance.

from requests_html import HTMLSession

session = HTMLSession()

r = session.post('https://api.anycaptcha.com/getBalance', data = {'clientKey': API})

Everything works. When I do print(r.text) the following dictionary gets print:

{"errorId":0,"errorCode":"SUCCESS","balance":20.0}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However, I just want to get the value of "balance" (20.0). But I couldn’t figure out how.

What I tried:

print(r.text['balance'])
# Output:

Traceback (most recent call last):
  File "X/test.py", line 7, in <module>
    print(r.text['balance'])
TypeError: string indices must be integers

>Solution :

Try this, as r.text is a string with your dict

import json
d = json.loads(r.text)

#Now you can access dict
d["balance"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading