无法刮取etherscan事务URL-cloudflare保护



刮取etherscan事务块id 的代码

def block_chain_explorer_block_id(url):
import requests
from bs4 import BeautifulSoup

r = requests.get(url)
soup = BeautifulSoup(r.content, 'html5lib')
tags = soup.findAll('div', attrs = {'class':'col-md-9'}) 
print(soup.findAll('a'))
block_chain_explorer_block_id(https://etherscan.io/tx/0x4529e9f79139edab871a699df455e57101cca90574e435da89db457df4885c54)

输出获取:

[<a href="https://www.cloudflare.com/5xx-error-landing" id="brand_link" rel="noopener noreferrer" target="_blank">Cloudflare</a>]

我得到以上输出polygonscan工作正常。etherscan运行良好。知道如何让它工作吗?

Etherscan有一个API(带有免费计划(。

您应该使用它,而不是试图刮取它,以下是交易文档:https://docs.etherscan.io/api-endpoints/stats

在请求中添加一些头,以显示您可能是一个">浏览器";可以提供短暂的缓解,但它远非防弹。

您还应该考虑访问目标页面的频率和速度。

使用旋转代理也是一种常见的方法。

注意 这没有什么神奇的公式,因为Cloudflare正在不断调整其检测机器人流量的方法。-使用@Speedlulu提到的api将是最佳方法

示例

添加了user agent作为标头之一,还将findAll()更改为find_all(),因为这是您应该在新代码中使用的语法。

import requests
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
def block_chain_explorer_block_id(url):
import requests
from bs4 import BeautifulSoup

r = requests.get(url,headers=headers)
soup = BeautifulSoup(r.content, 'html5lib')
tags = soup.find_all('div', attrs = {'class':'col-md-9'}) 
print(soup.find_all('a'))
block_chain_explorer_block_id('https://etherscan.io/tx/0x4529e9f79139edab871a699df455e57101cca90574e435da89db457df4885c54')

最新更新