首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ropsten网络中禁止Etherscan API请求403

Ropsten网络中禁止Etherscan API请求403
EN

Stack Overflow用户
提问于 2021-09-24 09:06:12
回答 1查看 554关注 0票数 0

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

代码语言:javascript
运行
复制
response = requests.get(
    "https://api-ropsten.etherscan.io/api",
    params={
        "module": "account",
        "action": "balance",
        "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
        "tag": "latest",
        "apikey": "MyApiKey",
    },
)

这是非常笨拙的,因为当我在Postman中对这个url执行相同的操作时,它可以工作:

代码语言:javascript
运行
复制
https://api-ropsten.etherscan.io/api?module=account&action=balance&address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae&tag=latest&apikey=MyApiKey

而且,当我向Ethereum发出同样的请求时,它也能正常工作:

代码语言:javascript
运行
复制
response = requests.get(
    "https://api.etherscan.io/api",
    params={
        "module": "account",
        "action": "balance",
        "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
        "tag": "latest",
        "apikey": "MyApiKey",
    },
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-08 08:52:25

我也在为同样的问题苦苦挣扎。对于其他正在苦苦挣扎的人,我找到了here的答案。本质上,Etherscan阻塞的是不提供User-agent的请求,因此如果使用User-agent请求模块,则添加Python header属性。

代码语言:javascript
运行
复制
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'})
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69312369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档