【问题标题】:Unable to scrape etherscan transaction urls - cloudflare protection无法抓取 etherscan 交易 url - cloudflare 保护
【发布时间】:2021-12-29 09:32:02
【问题描述】:

获取 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 工作正常。 知道如何让它工作吗?

【问题讨论】:

  • 它是刮,刮,刮 - 废料是完全不同的东西
  • 我也遇到了问题,你有什么问题吗?

标签: python beautifulsoup python-requests etherscan


【解决方案1】:

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

您应该使用它而不是尝试抓取它,这是 Transactions 的文档:https://docs.etherscan.io/api-endpoints/stats

【讨论】:

  • api非常有限
【解决方案2】:

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

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

使用轮换代理也是一种常见的方法。

注意 这没有什么神奇的公式,因为 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')

【讨论】:

  • 这在我的 macbook 中工作,但在 ubuntu 服务器中不工作
猜你喜欢
  • 2022-01-07
  • 2023-01-14
  • 2022-07-25
  • 2021-06-25
  • 2021-08-10
  • 2022-01-05
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
相关资源
最近更新 更多