I was planning on writing code that would get stock data from the exchange and store it for use and analysis later. I'm new to programming and Python so I don't really know where to get started. Are there functions, modules, or techniques I should be aware of? So far I'm only familiar with conditional programming, defining functions/objects, recursive loops, working with strings, reading from and writing to text files, etc. Just the basics really. What else would I have to learn to get started on a project like this? All help is appreciated. Thanks!

-- ThisIsNotAnId.

Use an online resource that makes it easy to retrieve data. Many sites have an API (Application Programming Interface) that allow programs to retrieve data with a few function calls.

For instance, Yahoo lets you retrieve a csv file of stock quotes with a url.
You can use the urllib.retrieve module to retrieve the data, and the csv module to process it.

#Written for and tested on Python 3.1.
import csv
import urllib.request

def yahoo_quotes_csv_url(tickers):
    return ''.join(('http://finance.yahoo.com/d/quotes.csv?s=',
                    '+'.join(tickers),
                    '&f=snl1'))

if __name__ == '__main__':
    tickers = ('GOOG', 'AAPL')
    url = yahoo_quotes_csv_url(tickers)
    print(url)
    urllib.request.urlretrieve(url, 'quotes.csv')
    reader = csv.reader(open('quotes.csv'))
    for row in reader:
        #row[0]=ticker, row[1]=company, float(row[2])=value
        print(row)

Different sites will provide different interfaces and have more data, but if you remember to read the documentation about retrieving data and if you remember to use the right modules you can use it.

commented: Very helpful! +2

As postet over using site API can help.

For webscraping are BeautifulSoup and lxml good parser.
Just a quick example with BeautifulSoup,take out price of bean.

#Python 2.x
from BeautifulSoup import BeautifulSoup
import urllib2

url = urllib2.urlopen('http://beans.itcarlow.ie/prices.html')
soup = BeautifulSoup(url)
#print soup #site conteds

tag = soup.find('strong')
print tag  #[<strong>$6.36</strong>]
print tag.string  #$6.36
commented: Provided helpful information. +2

Thanks fellas! Now to test the code!

Google finance has a "Get quotes" function.
Here is a demo how we can use this function with python.
To get data here i use regular expression.
Regular expression and html can be hard,thats why parser often are better to use.
Will see if i write a BeautifulSoup to.

import re, urllib

def get_quote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    find_q = re.search(r'\<span\sid="ref_\d+.*">(.+)<', content)
    if find_q:
        quote = find_q.group(1)
    else:
        quote = 'no quote available for: %s' % symbol
    return quote

def main():
    #print get_quote('ibm') #168.28

    #Test of 5 companys
    Companys = ['google', 'ibm', 'microsoft', 'apple', 'nvidia']
    for i,value in enumerate(Companys):
        print '%s --> %s' %  (Companys[i],get_quote(value))

        '''Output-->
        google --> 525.10
        ibm --> 168.28
        microsoft --> 25.52
        apple --> 350.70
        nvidia --> 18.52
        '''

if __name__ == "__main__":
    main()

Snippsat: cleaned little your code, as loop was little strange:

import re, urllib

def get_quote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    find_q = re.search(r'\<span\sid="ref_\d+.*">(.+)<', content)
    return find_q.group(1) if find_q else 'no quote available for: %s' % symbol

def print_quotes(companys):
    for company in companys:
        print '%s --> %s' %  (company,get_quote(company))

if __name__ == "__main__":
    #Test of 5 companys
    print_quotes(['google', 'ibm', 'microsoft', 'apple', 'nvidia'])

Best would be to check Google's Python API for access.

Thanks for all the info everyone! I didn't know about urllib or re before, but it turns out they can be used for a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.