Obtaining quotes from the stock market
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.
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
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
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
Thanks fellas! Now to test the code!
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
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
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
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.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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!
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0