Get data from the Ethereum API list
In this article, we will study how to access the data from the API responses list and extract important information.
Overview of the task
The task includes the creation of the Python script, which provides the list of API response objects as a starter. Each object has two pores of the value of the keys: “symbols” and “lastprice”. Our goal is to print only the values of Price, assuming that these are the prices we dominate.
necessary conditions
Before continuing, make sure you have:
- Python 3.x installed on your system.
- Creators.binance.com/docs/api-reference).
Implementation of the scenario
`Python
Import the required libraries
Import surveys
Define the list of API response objects
Price_list = [
{'Symbol': 'Ethbtc', 'Lastprice': '0.03574700'},
ethbtc
{'symbol': 'btcusdt', 'lastprice': '57621.0800000000'}
btcusdt
]]
Use the questions library to send a GET request to the end of the API
Def Get_api_response (data, result):
"" Send the request for a request to the rear point of the API and return the response data. "" ""
Answer = survey.get (event, for days = data)
Return the answer.json ()
Pull the Lastprice values from each API response object
For the article Price_list:
Print (f "Last {element ['symbol']} Price: {element ['Lastprice']})
Use an understanding list to extract and print only the values of the last prior
Last_prices = [Item ['Lastprice'] for the element price_list if 'btcusdt' element ['symbol']]]]]
Printing (latest prices: ")
For a "Last_price" price:
Print (price)
'
How it works
- The scenario imports the question library, which allows us to send HTTP requests to the end of the API.
- We define the list of API (Price_List) response objects, which contains two pairs of key values: "symbol" and "lastprice".
- We use the inserted functionality called "Get_api_respons (), which sends the Get request to the specified API end, using the question library. The termination point setting is transmitted as a mot- Key containing the URL of the final point of the main API.
- In the function "Get_api_respons (), we analyze the JSON response data using the JSON ()" method.
- Then we draw the Lastprit values from each API response object and print them in a console using an understanding of the loop and the list inserted.
- Finally, we use the list of the list to draw all the elements that do not contain BTCUSDT.
Expansion sample
'
Last price:
57621.08000000
0.03574700
Last price:
57621.08000000
` ‘
This scenario is an easy way to obtain data from the Ethereum API list and to withdraw specific values, which makes them useful for a variety of programs such as data analysis or web rating.