I am trying to scrape quotes from google finance's new site as the old one going to be deprecated soon. I have written some code to extract stock quotes, but it is painfully slow and takes about 2 minutes to return a single quote and returns only a few quotes every time i run the program.
import urllib
import re
import time
def get_quote(symbol):
base_url = 'http://google.com/finance?q='
content = urllib.urlopen(base_url + symbol).read()
m = re.search('id="ref_(.*?)">(.*?)<', content)
if m:
quote = m.group(2)
print quote,m
else:
quote = 'no quote available for: ' + symbol
return quote
while True:
get_quote('AMZN')
Output:
1,500.00 <_sre.SRE_Match object at 0x109f66360>
1,500.00 <_sre.SRE_Match object at 0x109f66360>
1,500.00 <_sre.SRE_Match object at 0x109f66360>
If you print variable m each time the loops, you will see that most of the time it will return value 'none'
How do I fix this?