Get startedGet started for free

Scraping the NASDAQ

Training neural nets is expensive - invest in NVIDIA! To find the best time to invest, collect stock data.

The context manager stock('NVDA') connects to NASDAQ and returns an object that you can use to get the latest price by calling its .price() method. You want to connect to stock('NVDA') and record 10 timesteps of price data by writing it to the file NVDA.txt.

You will notice the use of an underscore when iterating over the for loop. If this is confusing to you, don't worry. It could easily be replaced with an index i. But since we will not be using this index, we are using a dummy operator, _, which doesn't use any additional memory.

This exercise is part of the course

Writing Functions in Python

View Course

Exercise instructions

  • Use the stock('NVDA') context manager and assign the result to nvda.
  • Open a file for writing with open('NVDA.txt', 'w') and assign the file object to f_out so you can record the price over time.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Use the "stock('NVDA')" context manager
# and assign the result to the variable "nvda"
____ ____ ____ ____:
  # Open "NVDA.txt" for writing as f_out
  ____ ____ ____ ____:
    for _ in range(10):
      value = nvda.price()
      print('Logging ${:.2f} for NVDA'.format(value))
      f_out.write('{:.2f}\n'.format(value))
Edit and Run Code