How to create a stock quote fetching app in python

后端 未结 8 854
闹比i
闹比i 2020-12-07 22:05

I\'m quite new to programming in Python.

I want to make an application which will fetch stock prices from google finance. One exam

8条回答
  •  情深已故
    2020-12-07 22:40

    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.

提交回复
热议问题