【问题标题】:Cant use get_chart function in blockchain python client无法在区块链 python 客户端中使用 get_chart 函数
【发布时间】:2021-08-28 00:23:31
【问题描述】:

尝试运行

import blockchain
from blockchain import statistics

get_chart(chart_type="mempool-size", time_span="1year")

尽管get_chart 已在statistics.py 文件中为blockchain.info 客户端定义,但仍给出函数未定义错误。如何运行get_chart 函数?

有没有人有任何故障排除的想法?问题已经提出了几天,我被困住了。我检查了 GitHub 存储库中的问题,但找不到任何问题,还没有尝试任何其他方法,因为我不确定从哪里开始。

我对任何可以从 https://blockchain.info 获取图表数据的 python 解决方案都很满意

【问题讨论】:

  • 尝试将其称为statistics.get_chart(chart_type="mempool-size", time_span="1year"),它应该可以工作。或者,您可以以from blockchain.statistics import get_chart 的身份进行导入,然后按原样保持通话。

标签: python blockchain.info-api


【解决方案1】:

正如你所说,get_chart 是在blockchain.statistics 中定义的,但是导入statistics 模块确实会将其成员带入全局命名空间。你必须点掉它才能访问它的成员,例如get_chart

from blockchain import statistics

statistics.get_chart(chart_type="mempool-size", time_span="1year")

或者你可以直接导入函数:

from blockchain.statistics import get_chart

get_chart(chart_type="mempool-size", time_span="1year")

不幸的是,这并不能解决手头的更大问题,即软件包的存储库似乎已被放弃。对于您的请求,它会尝试从 URL https://blockchain.info/charts/mempool-size?format=json&timespan=1year 访问数据,这会导致下载 HTML 页面而不是 JSON。

不过,您仍然可以使用此处提供的文档访问图表 API:https://www.blockchain.com/api/charts_api

对于您的请求,要使用的正确 URL 是:https://api.blockchain.info/charts/mempool-size?format=json&timespan=1year

您可以下载它并将 JSON 解析为字典,如下所示:

import json
from urllib.request import urlopen

url = 'https://api.blockchain.info/charts/mempool-size?format=json&timespan=1year'
data = json.loads(urlopen(url).read())

【讨论】:

  • 尽管修复了我的无点错误并使用您上面编写的代码,但我收到 JSONDecodeError: Expecting value: line 1 column 1 (char 0),无论我请求哪个图表或时间跨度.有任何解决这个问题的方法吗?你会检查一下你是否有同样的问题?
  • @EliBain 我已经更新了答案。希望这会有所帮助。
猜你喜欢
  • 2017-10-26
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多