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

如何用Python爬取OpenSea网页中的ETH商品价格?

如何用Python获取OpenSea商品的ETH价格

OpenSea的商品价格是通过客户端动态渲染加载的,直接请求静态HTML拿不到数据,可采用以下两种方法解决:

方法一:调用OpenSea官方API(推荐,高效稳定)

OpenSea提供公开API接口,可直接获取商品列表和价格数据,无需渲染页面。

代码示例

import requests

# 目标API接口,对应collection的listing数据
api_url = "https://api.opensea.io/v2/listings/collection/beanzofficial/all"
# 构造匹配页面筛选条件的参数
params = {
    "payment_currency": "ETH",
    "limit": 20,
    "filters[traits][Background]": "Off White C",
    "order_direction": "asc"
}
# 模拟浏览器请求头
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"
}

# 发送请求并解析JSON
response = requests.get(api_url, params=params, headers=headers)
data = response.json()

# 提取首个商品的价格
first_item = data["listings"][0]
# API返回价格单位为wei(ETH最小单位),转换为ETH
price_eth = float(first_item["price"]["current"]["value"]) / 10**18
token_id = first_item["token_id"]

print(f"Bean #{token_id} 的价格是 {price_eth} ETH")

说明

  • 注意控制请求频率,避免触发OpenSea的速率限制;
  • 可调整limit参数获取更多商品数据;
  • 若API接口有更新,需参考OpenSea官方文档调整参数。

方法二:使用无头浏览器渲染页面

如果不想处理API参数,可通过无头浏览器模拟浏览器加载页面,提取渲染后的元素内容。这里以Playwright为例:

代码示例

from playwright.sync_api import sync_playwright

target_url = "https://opensea.io/collection/beanzofficial?search[paymentAssets][0]=ETH&search[stringTraits][0][name]=Background&search[stringTraits][0][values][0]=Off%20White%20C&search[toggles][0]=BUY_NOW"

with sync_playwright() as p:
    # 启动无头Chrome浏览器
    browser = p.chromium.launch(headless=True)
    page = browser.new_page(
        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"
    )
    # 等待页面加载完成(networkidle表示网络请求基本停止)
    page.goto(target_url, wait_until="networkidle")
    
    # 通过CSS选择器定位首个商品的价格元素
    price_element = page.locator(".Price--amount").first
    price_text = price_element.text_content().strip()
    
    print(f"首个商品价格:{price_text}")
    
    browser.close()

说明

  • 需先安装依赖:pip install playwright,再执行playwright install安装浏览器驱动;
  • 这种方式效率较低,但适合复杂页面交互场景;
  • 若页面结构更新,需调整CSS选择器以匹配最新的价格元素。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:33:31