Reputation: 25
i want you want loop through the result from yahoo finance api to return the company name and stock prices for top gainers.
import requests
import pprint
url = "https://yahoo-finance15.p.rapidapi.com/api/yahoo/ga/topgainers"
querystring = {"start":"0"}
headers = {
'x-rapidapi-host': "yahoo-finance15.p.rapidapi.com",
'x-rapidapi-key': "9efd0f3e52mshd859f5daf34a429p11cb2ajsn2b0e421d681e"
}
response = requests.request("GET", url, headers=headers, params=querystring)
new_response = response.text
def new_stock(one_stock):
arranged = []
for items in (one_stock):
new_name = dict.get("regularMarketPrice")
result = arranged.append(one_stock)
return result
print(new_stock(new_response))
but it keeps returning this error
'File "monitor.py", line 27, in <module>
print(new_stock(new_response))
File "monitor.py", line 23, in new_stock
new_name = dict.get("regularMarketPrice")
TypeError: descriptor 'get' requires a 'dict' object but received a 'str'
Upvotes: 0
Views: 1219
Reputation: 142631
You have few mistakes
If you expect JSON data then use response.json()
instead of response.text
data = response.json()
.text
gives string which you would have to convert to python dictionary using module json
import json
data = json.loads(response.text)
You get nested data and you need to use data['quotes']
to get list with results.
for item in data['quotes']:
#print(item.keys())
name = item.get("shortName")
price = item.get("regularMarketPrice")
append()
doesn't create new list and it returns `None so
result = arranged.append(one_stock)
means
result = None
You should use
arranged.append(one_stock)
and later
return arranged
And you should append value which you get, not one_stock
arranged.append( (name, price) )
Working code
import requests
import pprint
# --- functions ---
def new_stock(data):
result = []
for item in data['quotes']:
#print(item.keys())
name = item.get("shortName")
price = item.get("regularMarketPrice")
result.append((name, price))
return result
# --- main ---
url = "https://yahoo-finance15.p.rapidapi.com/api/yahoo/ga/topgainers"
querystring = {"start":"0"}
headers = {
'x-rapidapi-host': "yahoo-finance15.p.rapidapi.com",
'x-rapidapi-key': "9efd0f3e52mshd859f5daf34a429p11cb2ajsn2b0e421d681e"
}
response = requests.request("GET", url, headers=headers, params=querystring)
data = response.json()
#pprint.pprint(data)
pprint.pprint(new_stock(data))
Result:
[('ROLLS ROYCE HOLDINGS NON CUM RE', 0.0048),
('CIELO SA', 0.914),
('ASX LTD', 51.45),
('FLIR Systems, Inc.', 42.735),
('EQUATORIAL ENERGIA SA', 3.87),
('LOCALIZA RENT A CAR SA', 6.961),
('ADYEN NV UNSPON ADS EACH REP 0.', 18.64),
('Beyond Meat, Inc.', 84.35),
('SIKA AG', 17.025),
('Equifax, Inc.', 133.01),
('FUJI ELECTRIC CO. LTD.', 5.88),
('New Residential Investment Corp', 5.265),
('ENAGAS SA', 10.8301),
('THAI BEVERAGE PUBLIC COMPANY LT', 0.4749),
('ManpowerGroup', 64.61),
('Viela Bio, Inc.', 40.8),
('BANCO ACTINVER SA', 0.8351),
('Simmons First National Corporat', 18.3182),
('Suzano S.A.', 6.75),
('New Oriental Education & Techno', 113.04),
('PEUGEOT SA', 12.7148),
('The Madison Square Garden Compa', 237.22),
('FLUTTER ENTERTAINMENT PLC UNSPO', 56.65)]
Upvotes: 1