以太网扫描API请求403在Ropsten网络中被禁止

2024-10-05 13:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

这是非常尴尬的,因为当我从邮递员那里用这个url做同样的事情时,它会起作用:

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

而且,当我对以太坊Mainnet执行相同的请求时,它也可以工作:

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

Tags: httpsioapiaddressresponsetagactionaccount
1条回答
网友
1楼 · 发布于 2024-10-05 13:25:27

我也在为同样的问题挣扎。对于其他正在挣扎的人,我找到了答案。本质上,Etherscan正在阻止不提供User-agent的请求,因此如果使用Python请求模块,请添加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'})

相关问题 更多 >

    热门问题