'Etherscan api on ropsten for balance checking does not work

I have a problem with the etherscan api on ropsten testnetwork, the output of the code is: expecting value line 1 column 1 (char 0)

the code:

import requests, json

ADD = "0xfbb61B8b98a59FbC4bD79C23212AddbEFaEB289f"
KEY = "HERE THE API KEY"


REQ = requests.get(f"https://api-ropsten.etherscan.io/api?module=account&action=balance&address={str(ADD)}&tag=latest&apikey={str(KEY)}")

CONTENT = json.loads(REQ.content)
BALANCE = int(CONTENT['result'])

print(BALANCE)

EDIT: I researched it futher and when I try to do a request it gives back <Response [403]>



Solution 1:[1]

so i figured it out,

some websites dont allow python scripts to access there website, you can get around this by adding user agent in you request. the code would look something like this:

import requests, json

ADD = "0xfbb61B8b98a59FbC4bD79C23212AddbEFaEB289f"
KEY = "HERE THE API KEY"
LINK = f"https://api-ropsten.etherscan.io/api?module=account&action=balance&address={str(ADD)}&tag=latest&apikey={str(KEY)}"
headers = {"HERE YOUR USER-AGENT"}

REQ = requests.get(LINK, headers = headers)

CONTENT = json.loads(REQ.content)
BALANCE = int(CONTENT['result'])

print(BALANCE)

To find your user agent simply type in google: my user agent

if you have any questions feel free to ask.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arthur