You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从PolygonScan网页抓取解码输入数据表格的技术求助

解决Polygonscan未验证合约交易的解码输入数据抓取问题

核心问题原因

未验证合约的解码输入数据并非页面初始HTML的一部分,而是Polygonscan前端通过调用内部签名匹配服务(比如4byte.directory这类接口)动态渲染的。静态解析HTML或直接用API无法获取,必须模拟前端交互触发加载,再抓取动态生成的内容。

解决方案代码(基于requests_html)

使用requests_html的无头浏览器功能,模拟点击「See More」和「Decode Input Data」按钮,等待动态内容加载后提取表格数据:

from requests_html import HTMLSession

def fetch_decoded_input(tx_hash):
    url = f"https://polygonscan.com/tx/{tx_hash}"
    session = HTMLSession()
    
    # 添加浏览器UA避免反爬拦截
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
    }
    
    r = session.get(url, headers=headers)
    # 首次渲染加载页面基础内容
    r.html.render(sleep=2)
    
    # 点击「See More」展开完整输入数据
    see_more_btn = r.html.find('button.btn-link.btn-sm', containing='See More', first=True)
    if see_more_btn:
        see_more_btn.click()
        r.html.render(sleep=1)  # 等待页面更新
    
    # 点击「Decode Input Data」触发解码请求
    decode_btn = r.html.find('button.btn-link.btn-sm', containing='Decode Input Data', first=True)
    if decode_btn:
        decode_btn.click()
        r.html.render(sleep=2)  # 等待解码数据加载完成
    
    # 提取解码后的表格数据
    decode_table = r.html.find('#decodebox table.table.table-sm.table-hover', first=True)
    if not decode_table:
        return "未抓取到解码数据,可能是页面结构更新或反爬拦截"
    
    decoded_result = {}
    for row in decode_table.find('tr'):
        cols = row.find('td')
        if len(cols) == 2:
            decoded_result[cols[0].text.strip()] = cols[1].text.strip()
    
    session.close()
    return decoded_result

# 测试目标交易哈希
target_tx = "0x0d2e262cd328f53977d166a5cd1df3a9510b533efc81c71faa626b75cb7826e0"
print(fetch_decoded_input(target_tx))

关键注意事项

  1. 依赖环境:确保已安装requests-html,首次运行会自动下载Chromium,若失败可手动安装pyppeteer:pip install pyppeteer
  2. 反爬适配:如果出现拦截,可增加sleep时长,或添加Cookie(从浏览器登录Polygonscan后复制Cookie到请求头)
  3. 选择器维护:Polygonscan页面结构可能更新,若按钮/表格选择器失效,可通过浏览器开发者工具重新定位元素:
    • 按钮可通过「文本包含」的方式匹配,比固定class更稳定
    • 解码表格的容器ID#decodebox是长期稳定的标识

内容的提问来源于stack exchange,提问作者barryb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:15:56