How to create a stock quote fetching app in python

后端 未结 8 859
闹比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:32

    This module comes courtesy of Corey Goldberg.

    Program:

    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_694653_l".*?>(.*?)<', content)
        if m:
            quote = m.group(1)
        else:
            quote = 'no quote available for: ' + symbol
        return quote
    

    Sample Usage:

    import stockquote
    print stockquote.get_quote('goog')
    

    Update: Changed the regular expression to match Google Finance's latest format (as of 23-Feb-2011). This demonstrates the main issue when relying upon screen scraping.

提交回复
热议问题