0

我正在尝试向 Ropsten 网络中的 Etherscan API 发送请求,但它无法正常工作,因为它显示 403 错误:

response = requests.get(
    "https://api-ropsten.etherscan.io/api",
    params={
        "module": "account",
        "action": "balance",
        "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
        "tag": "latest",
        "apikey": "MyApiKey",
    },
)

这很尴尬,因为当我从 Postman 使用这个 url 做同样的事情时,它可以工作:

https://api-ropsten.etherscan.io/api?module=account&action=balance&address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae&tag=latest&apikey=MyApiKey

而且,当我向以太坊主网发出同样的请求时,它也能正常工作:

response = requests.get(
    "https://api.etherscan.io/api",
    params={
        "module": "account",
        "action": "balance",
        "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
        "tag": "latest",
        "apikey": "MyApiKey",
    },
)
4

1 回答 1

3

我在同样的问题上苦苦挣扎。对于其他正在苦苦挣扎的人,我在这里找到了答案。本质上,Etherscan 会阻止不提供的请求,因此如果使用 Python requests 模块,请User-agent添加标头属性。User-agent

response = requests.get(
        "https://api-ropsten.etherscan.io/api",
        params={
            "module": "account",
            "action": "balance",
            "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
            "tag": "latest",
            "apikey": "API_KEY",
        },
        headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, '
                               'like Gecko) Chrome/50.0.2661.102 Safari/537.36'})
于 2021-10-08T08:52:24.763 回答