使用Python调用icy.tools GraphQL API获取趋势集合报错求助
解决Icy Tools GraphQL API获取趋势集合的错误
错误原因
你传递的TrendingCollections不是合法的GraphQL查询参数值。GraphQL要求query参数必须是完整的查询语句,而非仅类型名称。直接写TrendingCollections会被API判定为语法错误。
正确的实现方式
方式1:GET请求(需传递完整查询语句)
将完整的GraphQL查询作为query参数的值,通过params传递会自动处理URL编码:
import requests # 定义完整的GraphQL查询,指定需要获取的字段 query = """ query TrendingCollections { trendingCollections { name slug floorPrice volume24h # 可根据需求添加更多字段,参考官方文档 } } """ url = "https://graphql.icy.tools/graphql" response = requests.get(url, params={"query": query}) print(response.json())
方式2:POST请求(更推荐,适合复杂查询)
GraphQL接口通常更推荐用POST请求,将查询放在请求体中,避免URL长度限制:
import requests url = "https://graphql.icy.tools/graphql" payload = { "query": """ query TrendingCollections { trendingCollections { name slug floorPrice volume24h # 按需添加字段,比如ownerCount、volume7d等 } } """ } headers = {"Content-Type": "application/json"} response = requests.post(url, json=payload, headers=headers) print(response.json())
注意事项
- 必须指定
trendingCollections下的具体字段,不能只请求类型本身 - 若需要传递查询变量(比如时间范围、排序方式),可以在查询中定义变量并在payload中传入
内容的提问来源于stack exchange,提问作者Asterix
相关产品推荐
相关产品推荐

