#This example uses Python 2.7 and the python-request library. from requests import Request, Session import datetime from requests.exceptions import ConnectionError, Timeout, TooManyRedirects import json # https://coinmarketcap.com/api/documentation/v1/#section/Endpoint-Overview url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest' parameters = { 'start':'1', 'limit':'5000', 'convert':'USD' } headers = { 'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': 'b14dfc5b-ec9d-4d53-8af7-fab0e5971120', } # 輸出(Console & File) def output(str): if str is not None: print(str) fo.write(str + '\n') session = Session() session.headers.update(headers) try: response = session.get(url, params=parameters) data = json.loads(response.text) filename = 'C:/wamp64/www/NFTBoard/api/' + datetime.datetime.now().strftime("%Y%m%d%H%M") + '.txt' fo = open(filename, 'w+', encoding='utf-8') print('資料總筆數 = %s' % data['status']['total_count']) count = 1 # NFT資料總筆數 nft_total_count = 0 for d in data['data']: tags = d['tags'] # 檢查是否為NFT if 'collectibles-nfts' in tags: nft_total_count += 1 ''' {"id":2416,"name":"THETA","symbol":"THETA","slug":"theta","num_market_pairs":61,"date_added":"2018-01-17T00:00:00.000Z","tags":["media","collectibles-nfts","content-creation","video","huobi-capital"],"max_supply":1000000000,"circulating_supply":1000000000,"total_supply":1000000000,"platform":null,"cmc_rank":18,"last_updated":"2021-07-01T07:47:06.000Z","quote":{"USD":{"price":6.47444235747614,"volume_24h":226106772.68725023,"percent_change_1h":-0.78042098,"percent_change_24h":-5.36040215,"percent_change_7d":-6.02352245,"percent_change_30d":-11.54236731,"percent_change_60d":-40.7622901,"percent_change_90d":-45.50203437,"market_cap":6474442357.47614,"last_updated":"2021-07-01T07:47:06.000Z"}}} ''' id = d['id'] icon = 'https://s2.coinmarketcap.com/static/img/coins/64x64/' + str(id) + '.png' name = d['name'] symbol = d['symbol'] price = d['quote']['USD']['price'] if price >= 1: price = format(price, '.2f') elif price >= 0.1 and price < 1: price = format(price, '.4f') elif price >= 0.01 and price < 0.1: price = format(price, '.4f') elif price >= 0.001 and price < 0.01: price = format(price, '.6f') elif price >= 0.0001 and price < 0.001: price = format(price, '.8f') elif price >= 0.00001 and price < 0.0001: price = format(price, '.9f') else: price = "<$0.00001" percent_change_24h = format(d['quote']['USD']['percent_change_24h'], '.2f') percent_change_7d = format(d['quote']['USD']['percent_change_7d'], '.2f') market_cap = int(d['quote']['USD']['market_cap']) volume_24h = int(d['quote']['USD']['volume_24h']) circulating_supply = str(int(d['circulating_supply'])) + ' ' + symbol last_7d = "https://s3.coinmarketcap.com/generated/sparklines/web/7d/usd/" + str(id) + ".png" output('\n%s.%s' % (nft_total_count, symbol)) output('icon: %s' % icon) output('name: %s' % name) output('price: $%s' % price) output('percent_change_24h: %s%%' % percent_change_24h) output('percent_change_7d: %s%%' % percent_change_7d) output('market_cap: $%s' % market_cap) output('volume_24h: $%s' % volume_24h) output('circulating_supply: %s' % circulating_supply) output('last_7d: %s' % last_7d) output('NFT資料總筆數 = %s' % nft_total_count) except (ConnectionError, Timeout, TooManyRedirects) as e: print(e)