realpython / book2-exercises

Book 2 -- Exercises for the book
168 stars 203 forks source link

stock_download.py doesn't (seemingly) function as it should (p. 489) #82

Closed stonemirror closed 7 years ago

stonemirror commented 8 years ago

In the "Web Interaction" section beginning on page 489, the sample script stock_download.py doesn't (I believe) function as intended: rather than collecting some number of stock price samples into succeeding rows of a .csv file, it rewrites the file from scratch on each iteration of the loop by opening it for write ("wb") rather than append ("ab") in the middle of the while loop.

The potential problem with opening it for append access is that subsequent runs, even months apart, will just tack new data onto old data; if that's the behavior desired, just change the "wb" in the open() call to "ab" (although re-opening the file each time through the loop seems a little silly to me...)

I restructured the code, however, to create a new CSV file every time I run the script, but to open it outside the loop so it functions as expected, or at least as I'd have expected. I have it running five times, 30 seconds apart and, as I'd expect, I get five lines of data in my stocks.csv file, rather than just one line of data representing the final GET request...

with open('stocks.csv', 'wb') as code:
    while i < 5:
        base_url = 'http://download.finance.yahoo.com/d/quotes.csv'
        data = requests.get(base_url, params={'s': 'GOOG', 'f': 'sl1d1t1c1ohgv', 'e': '.csv'})
        code.write(data.content)
        i += 1
        if i < 5:
            time.sleep(30)
stonemirror commented 8 years ago

Also, on page 490, http://download.finance.yahoo.com (and http://download.finance.yahoo.com/q?s=GOOG, as shown in the illustration) return 404s.

After poking around for a few minutes, I haven't had any real luck finding myself a link from which I could cherry-pick these parameters, but what's currently in there doesn't actually work. The full link used in the script, however:

http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=sl1d1t1c1ohgv&e=.csv

does produce an appropriate CSV file for downloading...

mjhea0 commented 8 years ago

Updating the course now. I am almost to this chapter. Thanks for letting me know!

mjhea0 commented 7 years ago

Updated in v 2.0 of the courses. Coming week of 12/19/2016. https://github.com/realpython/about/blob/master/changelog.csv