Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Writing Functions in Python

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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))
Code bewerken en uitvoeren