DavidMS
Posts: 139
Joined: Fri May 25, 2012 8:48 pm
Contact: Website

getting share price info

Sun Oct 26, 2014 9:36 pm

I am working on app which displays a share prices, I have found a simple method to get basic information from yahoo but I am struggling to see how to apply it using Python

If I paste http://finance.yahoo.com/d/quotes.csv?s ... &f=sb2b3jk in to Chrome it will down load a small CSV file containing the basis share price info

The source page for this is http://www.blogbyben.com/2009/01/truly- ... k-api.html

I am not looking for anything upto the minute the typical 20 minute delayed info is fine
http://meanderingpi.wordpress.com/
11 Raspberry Pi's and counting....

BillStephenson
Posts: 254
Joined: Thu May 17, 2012 1:25 am
Location: SW Missouri Ozarks
Contact: Website

Re: getting share price info

Sun Oct 26, 2014 9:58 pm

A google search brings up some stuff...

https://www.google.com/search?&query=py ... ock+ticker

and there's some example code here:

https://pypi.python.org/pypi/ystockquote

User avatar
DougieLawson
Posts: 38442
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: getting share price info

Sun Oct 26, 2014 10:11 pm

Try this code

Code: Select all

import os
import time
from datetime import datetime
import csv
from urllib.request import urlopen
import io

def getSymbol(symbol):
   url = 'http://finance.yahoo.com/d/quotes.csv?s={0}&f=snd1l1yrc1'.format(symbol)
   symdata = urlopen(url)
   response = str(symdata.read().decode('utf-8'))
   comsep = csv.reader(response.split('\r\n'), delimiter='"')
   for row in comsep:
      try:
         row[0] = row[0].split(',')[0]
         row.append(row[4].split(',')[1])
         row.append(row[4].split(',')[2])
         row.append(row[4].split(',')[3])
         row.append(row[4].split(',')[4])
         row.append(datetime.now())
         symrow = row
      except:
         pass
   return symrow
it pulls the Yahoo finance data and parses it out. Input is a stock symbol name like "^FTSE" or "BG.L".
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DavidMS
Posts: 139
Joined: Fri May 25, 2012 8:48 pm
Contact: Website

Re: getting share price info

Sun Oct 26, 2014 10:59 pm

Thanks Dougie

This is just what I was looking for

- for those still on Python 2.x -

change

Code: Select all

urllib.request import urlopen
to

Code: Select all

urllib2 import urlopen
http://meanderingpi.wordpress.com/
11 Raspberry Pi's and counting....

Return to “Python”