這是一個簡單的模塊,可從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
}
}