I\'m quite new to programming in Python.
I want to make an application which will fetch stock prices from google finance. One exam
import urllib
import re
def get_quote(symbol):
base_url = 'http://finance.google.com/finance?q='
content = urllib.urlopen(base_url + symbol).read()
m = re.search('id="ref_(.*?)">(.*?)<', content)
if m:
quote = m.group(2)
else:
quote = 'no quote available for: ' + symbol
return quote
I find that if you use ref_(.*?) and use m.group(2) you will get a better result as the reference id changes from stock to stock.
I suggest using the HTMLParser to get the value of the meta tags google places in it's html
<meta itemprop="name"
content="Cerner Corporation" />
<meta itemprop="url"
content="https://www.google.com/finance?cid=92421" />
<meta itemprop="imageUrl"
content="https://www.google.com/finance/chart?cht=g&q=NASDAQ:CERN&tkr=1&p=1d&enddatetime=2014-04-09T12:47:31Z" />
<meta itemprop="tickerSymbol"
content="CERN" />
<meta itemprop="exchange"
content="NASDAQ" />
<meta itemprop="exchangeTimezone"
content="America/New_York" />
<meta itemprop="price"
content="54.66" />
<meta itemprop="priceChange"
content="+0.36" />
<meta itemprop="priceChangePercent"
content="0.66" />
<meta itemprop="quoteTime"
content="2014-04-09T12:47:31Z" />
<meta itemprop="dataSource"
content="NASDAQ real-time data" />
<meta itemprop="dataSourceDisclaimerUrl"
content="//www.google.com/help/stock_disclaimer.html#realtime" />
<meta itemprop="priceCurrency"
content="USD" />
With code like this:
import urllib
try:
from html.parser import HTMLParser
except:
from HTMLParser import HTMLParser
class QuoteData:
pass
class GoogleFinanceParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.quote = QuoteData()
self.quote.price = -1
def handle_starttag(self, tag, attrs):
if tag == "meta":
last_itemprop = ""
for attr, value in attrs:
if attr == "itemprop":
last_itemprop = value
if attr == "content" and last_itemprop == "name":
self.quote.name = value
if attr == "content" and last_itemprop == "price":
self.quote.price = value
if attr == "content" and last_itemprop == "priceCurrency":
self.quote.priceCurrency = value
if attr == "content" and last_itemprop == "priceChange":
self.quote.priceChange = value
if attr == "content" and last_itemprop == "priceChangePercent":
self.quote.priceChangePercent = value
if attr == "content" and last_itemprop == "quoteTime":
self.quote.quoteTime = value
if attr == "content" and last_itemprop == "exchange":
self.quote.exchange = value
if attr == "content" and last_itemprop == "exchangeTimezone":
self.quote.exchangeTimezone = value
def getquote(symbol):
url = "http://finance.google.com/finance?q=%s" % symbol
content = urllib.urlopen(url).read()
gfp = GoogleFinanceParser()
gfp.feed(content)
return gfp.quote;
quote = getquote('CSCO')
print quote.name, quote.price