0

I am very new to Python and looking for a way to get the current stock price of TESLA. I have this program but it prints out way too much. I am running python 3 connecting to the school computer via Linux. I can not install Pandas, pip requests or conda because I am not authorized. I even tried sudo. I was hoping to find an api something like this: http://api.bart.gov/api/route.aspx? but for tesla. I appreciate any help I can get. At this point ucllib seems to work getting the url.

import urllib.request, urllib.parse
params = { "key": "MW9S-E7SL-26DU-VV8V", "cmd": "routes" }
url = "https://finance.yahoo.com/quote/TSLA/history?period1=1436486400&period2=1594339200&interval=1d&filter=history&frequency=1d" + urllib.parse.urlencode(params)
with urllib.request.urlopen(url) as result:
  print(result.read())
  • If you only need the price then you can just use Excel. Type TESLA in a cell and click convert to Stock. – West Dec 11 '20 at 04:51

1 Answers1

1

Use https://query2.finance.yahoo.com/v10/finance/quoteSummary/tsla?modules=price

import urllib.request, json
resp = urllib.request.urlopen('https://query2.finance.yahoo.com/v10/finance/quoteSummary/tsla?modules=price')
data = json.loads(resp.read())
price = data['quoteSummary']['result'][0]['price']['regularMarketPrice']['raw']
print(price)
hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • I get this error. $ python3 stock6.py Traceback (most recent call last): File "stock6.py", line 8, in data = json.loads(resp.read()$ python3 stock6.py Traceback (most recent call last): File "stock6.py", line 8, in data = json.loads(resp.read() – user14798727 Dec 11 '20 at 05:12
  • Works for me. Perhaps you tried too many times and got throttled? The error indicates that what you got back wasn't proper JSON. Maybe inspect the response that you got; it might contain an error message with more information. – tripleee Dec 11 '20 at 05:21
  • Wow! This works perfect. Thanks a bunch. I have been working on this since Monday. – user14798727 Dec 11 '20 at 05:21