区块链API每4天只给我一次数据

问题描述 投票:0回答:1

尝试使用 Blockchain.com API 下载每日比特币矿工费用。然而,即使网站上的图表(https://www.blockchain.com/explorer/charts/transaction-fees)显示每日数据,API 也只提供每 4 天的数据。

import requests
import pandas as pd

url = "https://api.blockchain.info/charts/transaction-fees?timespan=all&format=json"

response = requests.get(url)
data = response.json()
df = pd.DataFrame(data['values'])

# Convert the timestamp to a readable format
df['x'] = pd.to_datetime(df['x'], unit='s')  # 'x' is the Unix timestamp
df.set_index('x', inplace=True)  # Set the datetime as the index
df.rename(columns={'y': 'fees'}, inplace=True)  # Rename 'y' to 'hashrate'

print(df.head())

我尝试将代码调整为滚动 4d 窗口并进行插值,但是当我这样做时,会出现相当大的错误,因为在这种情况下,我没有获得每天的正确数据,而是根据周围的日子来估计它:

import requests
import pandas as pd

url = "https://api.blockchain.info/charts/transaction-fees?timespan=all&rollingAverage=4days&format=json"

response = requests.get(url)
data = response.json()

df = pd.DataFrame(data['values'])

# Convert the timestamp to a readable format
df['x'] = pd.to_datetime(df['x'], unit='s')  
df.set_index('x', inplace=True)  
df.rename(columns={'y': 'fees'}, inplace=True)  # Rename 'y' to 'hashrate'
df_daily = df.resample('D').interpolate(method='linear')

print(df_daily.head())

--更新

我认为这是一个 API 限制。由于原始数据每次也会丢失 3 天:https://api.blockchain.info/charts/transaction-fees

--更新2 我已经在 api 请求中添加了“sampled=false”,现在我每 15 分钟获取一次数据,这太多了。我只是在寻找日常数据,但 API 文档不太好:

https://www.blockchain.com/explorer/api/charts_api

python blockchain
1个回答
0
投票

问题似乎是,当您请求太多记录时,API 会自动调整时间增量。作为解决方法,您可以使用循环:

import requests
import pandas as pd

url = 'https://api.blockchain.info/charts/transaction-fees'

timespan = 6 # up to 6 (years) gets you daily results, 
# from 7 it uses a timedelta of '2d'

# set params for get request
params = {'start': '2009-01-01',
          'timespan': f'{timespan}years',
          'format': 'json'}

# initialize list to collect values per request
values = []

for year in range(2009, 2024, timespan):
       
    request_params = params.copy()
    request_params['start'] = f'{year}-01-01'
    
    response = requests.get(url, params=request_params)
    
    data = response.json()

    # append values to `values`
    values.extend(data['values'])
    print(f'{year} added')

df = pd.DataFrame(values)
df['x'] = pd.to_datetime(df['x'], unit='s')  
df.set_index('x', inplace=True)  
df.rename(columns={'y': 'fees'}, inplace=True)

输出:

import numpy as np

df.iloc[np.r_[0:2, -2:0]]

                 fees
x                    
2009-01-17   0.000000
2009-01-18   0.000000
2024-10-12  44.805855
2024-10-13  24.719396

剧情:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(rc={'figure.figsize':(20, 10)})
sns.set(style="ticks")
sns.lineplot(data=df['fees'])
plt.grid()
plt.show()

plot data

实际上,如果您将其设置为“全部”,网站上的图表本身就会跳过几天。例如:

zoom chart website

将其与检索到的原始数据进行比较:

df.loc['2017-12-20':'2017-12-23']

                   fees
x                      
2017-12-20   911.591743
2017-12-21  1128.761670 # matches value as shown in snippet website (1,128.762)
2017-12-22  1495.946477 # peak, not shown on website
2017-12-23  1065.615595
© www.soinside.com 2019 - 2024. All rights reserved.