· 5 years ago · Jan 07, 2021, 06:40 AM
1base_url = "https://www.alphavantage.co/query?function=SECTOR"
2api = "SVJRMZSLCYF3A5WV"
3# main_url variable that stores complete url with API key
4main_url = base_url + "&apikey=" + api
5
6# get method of requests module
7# return response object
8req_ob = requests.get(main_url)
9# result contains list of nested dictionaries
10result = req_ob.json()
11sector_name = []
12sector_pct = []
13
14timeline_indice = ['Rank A: Real-Time Performance', 'Rank B: 1 Day Performance', 'Rank C: 5 Day Performance',
15 'Rank D: 1 Month Performance', 'Rank E: 3 Month Performance',
16 'Rank F: Year-to-Date (YTD) Performance',
17 'Rank G: 1 Year Performance', 'Rank H: 3 Year Performance', 'Rank I: 5 Year Performance',
18 'Rank J: 10 Year Performance']
19
20sector_indices = ['Consumer Discretionary', 'Energy', 'Communication Services', 'Information Technology',
21 'Consumer Staples', 'Health Care', 'Materials', 'Utilities', 'Industrials', 'Financials']
22
23for key in result:
24 if key == sector_indices[0]:
25 sector_name.setText(key)
26 sector_pct.setText(key)
27 elif key == sector_indices[1]:
28 sector_name.setText(key)
29 sector_pct.setText(key)
30 elif key == sector_indices[2]:
31 sector_name.setText(key)
32 sector_pct.setText(key)
33 elif key == sector_indices[3]:
34 sector_name.setText(key)
35 sector_pct.setText(key)
36 elif key == sector_indices[4]:
37 sector_name.setText(key)
38 sector_pct.setText(key)
39 elif key == sector_indices[5]:
40 sector_name.setText(key)
41 sector_pct.setText(key)
42 elif key == sector_indices[6]:
43 sector_name.setText(key)
44 sector_pct.setText(key)
45 elif key == sector_indices[7]:
46 sector_name.setText(key)
47 sector_pct.setText(key)
48 elif key == sector_indices[8]:
49 sector_name.setText(key)
50 sector_pct.setText(key)
51 elif key == sector_indices[9]:
52 sector_name.setText(key)
53 sector_pct.setText(key)
54
55print(sector_name)