0

I am trying to request data from Yahoo Finance and then print specific pieces of the data.

My code so far is:

import requests

ticker = input("Enter Stock Ticker: ")

url = "https://query1.finance.yahoo.com/v8/finance/chart/{}?region=GB&lang=en-GB&includePrePost=false&interval=2m&range=1d&corsDomain=uk.finance.yahoo.com&.tsrc=finance".format(ticker)
r = requests.get(url)
data = r.json()

What I am unsure of is how to extract certain pieces of the 'data' variable. For example, I want to display the value that is paired with 'regularMarketPrice'. This can be found in the request.

How can I do this?

Apologies if this isn't worded correctly.

Thanks

Patrick Miller
  • 335
  • 1
  • 3
  • 8
  • What have you tried in order to "*extract certain pieces of the 'data' variable*"? This is well-documented, both in the official documentation as well as on Stack Overflow. – esqew Oct 04 '20 at 21:22
  • Does this answer your question? [Selecting fields from JSON output](https://stackoverflow.com/questions/12934699/selecting-fields-from-json-output) – esqew Oct 04 '20 at 21:22

1 Answers1

0

If you print data, you will see that it is a dictionary.

If you dig deep enough into the dictionary, you will see that regularMarketPrice can be retrieved as follows (for the first result):

print(data['chart']['result'][0]['meta']['regularMarketPrice'])

If there are multiple results, then you can use the following:

for result in data['chart']['result']:
    print(result['meta']['regularMarketPrice'])
jarmod
  • 71,565
  • 16
  • 115
  • 122