Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
stonemirror opened this issue Oct 27, 2016 · 3 comments
Closed

Comments

@stonemirror
Copy link

stonemirror commented Oct 27, 2016

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
Copy link
Author

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
Copy link
Contributor

mjhea0 commented Nov 1, 2016

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

@mjhea0
Copy link
Contributor

mjhea0 commented Dec 17, 2016

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

@mjhea0 mjhea0 closed this as completed Dec 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants