这是一个简单的模块,可从Coin360.com和CoinMarketCap API中检索BTC中的即时加密货币值
首先安装软件包
pip install cryptocurrencies_scraper
from cryptocurrencies_scraper . CurrencyService import Manager
manager = Manager () # Create a new instance of the Manager
manager . update () # Ask the manager to update all currencies values
manager . all_currencies () # returns a dict that contain the currency name and symbol as key and currency information as a list
manager . get_curency ( 'btc' ) # return the list of currency values retrived for bitcoin using symbol
manager . get_curency ( 'bitcoin' ) # return the list of currency values retrived for bitcoin using name
刮板应扩展CurrentPriceInterface
。这是新刮板的基本方案
class MySourceForCurrencies ( CurrentPriceInterface ):
instance = None
URL = 'https://api.mysource.com/v1/ticker/?limit=3000'
NAME = "mysource.com"
@ classmethod
def get_instance ( cls ) -> 'MySourceForCurrencies' :
if cls . instance is None :
cls . instance = MySourceForCurrencies ()
return cls . instance
def __init__ ( self ):
super (). __init__ ()
def update_currency_list ( self ):
self . process_data ( requests . get ( self . URL ). json ())
def process_data ( self , json_data : json ) -> None :
# Manipulate the the json to be Currency "parsable"
c = Currency . parse_json ( json_data , self . NAME )
#When parsed, add it to the index
NameIndexes . get_instance (). add_to_index ( c )
return None
为了使您的刮板工作,您需要将其添加到经理
from cryptocurrencies_scraper . CurrencyService import Manager
manager = Manager ()
manager . add_source ( MySourceForCurrencies . get_instance ())
# you can also remove a source
manager . remove_source ( MySourceForCurrencies . NAME )
然后在每个manager.update()
上。
该类用于内部用来索引所有货币。当添加新货币时,它将按符号和名称(符号upper,name lose case)进行分组
唯一有用的方法是NameIndexes.get_instance().add_to_index(currency)
。在每个更新中,它可以从所有旧值中完整清除。
Currency
这是代表一种货币的对象是结构
{
"symbol" : " BTC " ,
"name" : " Bitcoin " ,
"valueUSD" : 5513 ,
"valueBTC" : 1 ,
"lastUpdate" : 1566531513813 ,
"source" : " coin360.com " ,
"changes" : {
"7d" : -1 ,
"24h" : -5 ,
"1h" : -10
}
}