Stock quote AP

ROGER GRAYDON CHRISTMAN dvl at psu.edu
Mon Mar 12 23:01:15 EDT 2018


On Mon, Mar 12, 2018 12:00 PM, Irv Kalb wrote:
>
> On Mar 10, 2018, at 9:26 PM, Chris Angelico <rosuav at gmail.com> wrote:
>> 
>> On Sun, Mar 11, 2018 at 4:18 PM, Irv Kalb <Irv at furrypants.com> wrote:
>> Hi,
>> 
>> I teach courses on beginning Python (Python3).  In one of my
>topics, I explain how we can write simple programs that reach out to the
>internet and download data (request/response).
>> 
>> I show a number of examples using:   urllib.request.urlopen(
><urlWIthParameters> )  to get things like weather data, currency
>exchange rates, etc.
>> 
>> I just tried my examples again, and they are all working fine, except for
>one.  I had an example where I used the call above to get simple
>(American) stock quotes from Yahoo.  However, with this example, now
>I get a bunch errors.  In tracking it down, I found that Yahoo has shut down
>this public API, discontinued this service.
>> 
>> So ... I am looking for a replacement.  I have done quite a bit of
>searching, but I have not been able to find a simple way to get a stock quote
>(no need for historical data - most recent price is fine).  I have
>found many examples where people have built custom packages for doing this type
>of thing.  However, I am in a college environment, and I cannot install any new
>packages on the computers there.  I've also seen examples of people building
>SQL-style queries to get this type of information, but that's beyond what I am
>trying to teach.
>> 
>> Wondering if anyone has any example of an API where I could just make a
>call using Python Standard Library interfaces to get stock quotes?
>> 
>> 
>> Check out https://www.alphavantage.co/ for something you can query for
>> free. Extensive and amazingly useful. One of my students did some
>> second-tier analysis on the data they provide as a capstone project on
>> stock trading analysis.
>> 
>> You may want to consider, though, modifying the "no new packages"
>> rule. The 'requests' library is WAY better for teaching Python and web
>> APIs than the raw urllib. Get just a small handful of pip-installable
>> packages whitelisted and your life will be better.
>> 
>> ChrisA
>> 
>
>Hi Chris,
>
>Thank you very much for this.  It is very close to what I am looking for.  I
>had seen this early in my searches but I didn't go into it in detail because it
>looked like it was designed to give way more information than I was looking for
>- for example, the first example is about time series data.  
>
>I did look into it today, and I got a free API key to check it out.  It does
>have the ability to give just a stock quote for a symbol, but it looks like the
>minimum I can get back is a csv:
>
>symbol,price,volume,timestamp
>MSFT,96.1800,--,2018-03-09 16:01:30
>
>which is easy enough for me to break apart.  I just wish there was a way to
>eliminate the header line so I wouldn't have to go through an explanation about
>that.  
>
>Thanks very much.  If I can't find another one that just give back a price,
>I'll probably use this one.
>
>Irv
>
>
>


Have you introduced the concepts of dictionaries in your Python course yet?
In my introductory course, I present dictionaries even before if statements and
for loops,
since they have so many very useful features for data manipulation.

That first line in the csv file is perfect for use with a dictionary.
You would just need to get a dictionary Reader from the csv module,

import urllib.request

import io, csv

file = urllib.request.urlopen(site + filename)

data = io.TextIOWrapper(file, newline="", encoding="utf-8")

reader = csv.DictReader(data)

for row in reader:


At this point, you could get your data from row['symbol'], row['price']
If you want to pretend that you don't actually know what the headings are,
you could simply enumerate that row dictionary value, at it will tell
you both the heading the value.   Each line in the file would be a new
dictionary.

Roger Christman
Pennsylvania State University






More information about the Python-list mailing list