forked from nitinkaushik01/Data_Science_Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extract_Data_using_APIs.py
107 lines (47 loc) · 1.54 KB
/
Extract_Data_using_APIs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# coding: utf-8
# ## Import Libraries
# In[41]:
import requests
import pandas as pd
from prettytable import PrettyTable
import json
# ## Create pretty table object
# In[42]:
tableobj = PrettyTable()
# ## Store API Key in a variable & concatenate it with API endpoint
# In[45]:
KeyVal = '37d0d5d1-eca3-4739-8723-8b4dce81be2b'
api_endpoint = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY='
api_endpoint += KeyVal
api_endpoint
# ## Fetch data in JSON format
# In[47]:
json_data = requests.get(api_endpoint).json()
cryptodata = json_data['data']
# In[50]:
requests.get(api_endpoint).json()
# ## Store data in Pretty Table
# In[51]:
for currency in cryptodata:
curr_name = currency['name']
curr_price = currency['quote']['USD']['price']
curr_change_1h = currency['quote']['USD']['percent_change_1h']
curr_change_24h = currency['quote']['USD']['percent_change_24h']
curr_change_7d = currency['quote']['USD']['percent_change_7d']
tableobj.add_row([curr_name, curr_price, curr_change_1h, curr_change_24h, curr_change_7d])
# ## Create External Field names and assign as column names in Pretty table
# In[52]:
tableobj.field_names = ["Currency Name", "Currency Price", "Currency 1h Change", "Currency 24h change", "Currency 7d change"]
# In[53]:
print(tableobj)
# ## Store data in Text File format
# In[54]:
table_txt = tableobj.get_string()
with open('output.txt', 'w') as file:
file.write(table_txt)
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]: