stock quotes off the web, py style

Chris Lindsay chris at open-cosmos.com
Wed May 16 09:01:06 EDT 2018


 >It serves a naked set of data, which happens to conform to the python
source code specification for dictionaries and consequently can be compiled
into a dictionary with 'eval', like so:

I would highly discourage any long-term usage (or any usage) of eval() in
this sort of context. If iextrading was compromised, a malicious third
party could simply start serving arbitrary python expressions (instead of
the dictionary-like data currently) and your script would execute it
unquestioningly.

Consider using ast.literal_eval() -
https://docs.python.org/3/library/ast.html#ast.literal_eval  for parsing
string representations of basic python datatypes.

On 16 May 2018 at 13:33, Friedrich Rentsch <anthra.norell at bluewin.ch> wrote:

>
>
> On 05/16/2018 02:23 AM, Mike McClain wrote:
>
>>      Initially I got my quotes from a broker daily to plug into a
>> spreadsheet, Then I found Yahoo and wrote a perl script to grab them.
>> When Yahoo quit supplying quotes I found AlphaVantage.co and rewrote
>> the perl script.
>>      AlphaVantage.co has been down since last week and I found
>> iextrading.com has a freely available interface. Since it needs
>> a rewrite and I'm trying to get a handle on python this seems
>> like a good opportunity to explore.
>>      If someone would please suggest modules to explore. Are there any
>> upper level modules that would allow me to do something like:
>>
>> from module import get
>> def getAquote(symbol):
>>      url = 'https://api.iextrading.com/1.0/stock/()/quote'.format(symbol)
>>      reply = module.get(url)
>>      return my_parse(reply)
>>
>> Thanks,
>> Mike
>> --
>> Men occasionally stumble over the truth, but most of them pick
>> themselves up and hurry off as if nothing ever happened.
>>      - Churchill
>>
>
> I didn't know the site you mention. I've been getting quotes from Yahoo
> daily. The service they discontinued was for up to 50 symbols per page. I
> now parse a separate page of some 500K of html for each symbol! This site
> is certainly more concise and surely a lot faster. It serves a naked set of
> data, which happens to conform to the python source code specification for
> dictionaries and consequently can be compiled into a dictionary with
> 'eval', like so:
>
> >>> ibm = urllib2.urlopen ("https://api.iextrading.com/1.0/stock/IBM/quote
> ").read()
> >>> ibm = eval (ibm)
> >>> for item in sorted (ibm.items()): print '%-24s%s' % item
>
> avgTotalVolume          5331869
> calculationPrice        close
> change                  -0.56
> changePercent           -0.00388
> close                   143.74
> closeTime               1526414517398
> companyName             International Business Machines Corporation
> delayedPrice            143.74
> delayedPriceTime        1526414517398
> high                    143.99
> iexAskPrice             0
> iexAskSize              0
> iexBidPrice             0
> iexBidSize              0
> iexLastUpdated          0
> iexMarketPercent        0
> iexRealtimePrice        0
> iexRealtimeSize         0
> iexVolume               0
> latestPrice             143.74
> latestSource            Close
> latestTime              May 15, 2018
> latestUpdate            1526414517398
> latestVolume            4085996
> low                     142.92
> marketCap               131948764304
> open                    143.5
> openTime                1526391000646
> peRatio                 10.34
> previousClose           144.3
> primaryExchange         New York Stock Exchange
> sector                  Technology
> symbol                  IBM
> week52High              171.13
> week52Low               139.13
> ytdChange               -0.0485148849103
>
> You would do multiple symbols in a loop which you enter with an open
> urllib object, rather than opening a new one for each symbol inside the
> loop.
>
> Frederic
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Chris
Open Cosmos

Any opinions given above are my own.



More information about the Python-list mailing list