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

如何过滤Python GET请求响应仅获取OpenSea的floor price?

问题

我从opensea.io获取统计数据,但仅关注floor price。我尝试在requests.get中添加payload参数payload = {'floor_price': ' '},但仍返回全部统计数据。以下是我的代码及响应结果:

import requests
headers = {"accept": "application/json"}
slug = 'doodles-official'
baseurl = 'https://api.opensea.io/api/v1/collection/'
addon = '/stats'
doodles = baseurl+slug+addon
payload = {'floor_price': ' '}
r = requests.get(doodles, headers=headers, params=payload)

print(r.text)

响应结果:

{"stats":{"one_hour_volume":6.99,"one_hour_change":0.0,"one_hour_sales":1.0,"one_hour_sales_change":0.0,"one_hour_average_price":6.99,"one_hour_difference":6.99,"six_hour_volume":20.834000000000003,"six_hour_change":0.0,"six_hour_sales":3.0,"six_hour_sales_change":0.0,"six_hour_average_price":6.944666666666667,"six_hour_difference":20.834000000000003,"one_day_volume":27.834000000000003,"one_day_change":-0.016834683828671863,"one_day_sales":4.0,"one_day_sales_change":0.0,"one_day_average_price":6.958500000000001,"one_day_difference":-0.4765999999999977,"seven_day_volume":309.110982,"seven_day_change":0.45300639422315325,"seven_day_sales":42.0,"seven_day_average_price":7.359785285714286,"seven_day_difference":96.37208199999998,"thirty_day_volume":1279.7464910000008,"thirty_day_change":-0.47290656802367925,"thirty_day_sales":156.0,"thirty_day_average_price":8.203503147435903,"thirty_day_difference":-1148.1845234344428,"total_volume":150854.48212122018,"total_sales":24131.0,"total_supply":10000.0,"count":10000.0,"num_owners":5401,"average_price":6.251480755924751,"num_reports":1,"market_cap":73597.85285714285,"floor_price":6.8}}

Process finished with exit code 0

我期望仅返回floor_price,但实际仍得到全部统计数据,该如何解决?

解决方案

OpenSea的/collection/{slug}/stats接口不支持通过URL参数过滤返回字段,你添加的floor_price参数对接口没有作用,所以会返回完整的统计数据。要只获取floor_price,需要在拿到响应后自行提取对应字段:

  1. 将响应文本解析为JSON对象
  2. 从JSON中提取stats节点下的floor_price值

修改后的代码如下:

import requests
headers = {"accept": "application/json"}
slug = 'doodles-official'
baseurl = 'https://api.opensea.io/api/v1/collection/'
addon = '/stats'
doodles = baseurl+slug+addon

# 无需添加无效的payload参数
r = requests.get(doodles, headers=headers)

# 解析响应为JSON格式
stats_data = r.json()
# 提取目标字段floor_price
floor_price = stats_data['stats']['floor_price']

print(floor_price)

运行后会直接输出6.8,满足仅获取floor_price的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 11:01:10