How to create a stock quote fetching app in python

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

    http://docs.python.org/library/urllib.html for fetching arbitrary URLs.

    Apart from that you should better look a some web service providing the data in JSON format.

    Otherwise you have to implement parsing etc. on your own.

    Screenscrapping yahoo.com for getting the stocks is unlikely the right road to success.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 22:33

    Just in case you want to pull data from Yahoo... Here is a simple function. This does not scrape data off a normal page. I thought I had a link to the page describing this in the comments, but I do not see it now - there is a magic string appended to the URL to request specific fields.

    import urllib as u
    import string
    symbols = 'amd ibm gm kft'.split()
    
    def get_data():
        data = []
        url = 'http://finance.yahoo.com/d/quotes.csv?s='
        for s in symbols:
            url += s+"+"
        url = url[0:-1]
        url += "&f=sb3b2l1l"
        f = u.urlopen(url,proxies = {})
        rows = f.readlines()
        for r in rows:
            values = [x for x in r.split(',')]
            symbol = values[0][1:-1]
            bid = string.atof(values[1])
            ask = string.atof(values[2])
            last = string.atof(values[3])
            data.append([symbol,bid,ask,last,values[4]])
        return data
    

    Here, I found the link that describes the magic string: http://cliffngan.net/a/13

    0 讨论(0)
  • 2020-12-07 22:33

    You can start by looking at the Google Finance APIs, although I don't see a Python API or wrapper. It looks like the only options for accessing the data directly are Java and JavaScript. You can also use cURL if you're familiar with it and it's available on your system.

    0 讨论(0)
  • 2020-12-07 22:37

    As for now (2015), the google finance api is deprecated. But you may use the pypi module googlefinance.

    Install googlefinance

    $pip install googlefinance
    

    It is easy to get current stock price:

    >>> from googlefinance import getQuotes
    >>> import json
    >>> print json.dumps(getQuotes('AAPL'), indent=2)
    [
      {
        "Index": "NASDAQ", 
        "LastTradeWithCurrency": "129.09", 
        "LastTradeDateTime": "2015-03-02T16:04:29Z", 
        "LastTradePrice": "129.09", 
        "Yield": "1.46", 
        "LastTradeTime": "4:04PM EST", 
        "LastTradeDateTimeLong": "Mar 2, 4:04PM EST", 
        "Dividend": "0.47", 
        "StockSymbol": "AAPL", 
        "ID": "22144"
      }
    ]
    

    Google finance is a source that provides real-time stock data. There are also other APIs from yahoo, such as yahoo-finance, but they are delayed by 15min for NYSE and NASDAQ stocks.

    0 讨论(0)
  • 2020-12-07 22:37

    Another good place to start is Google Finance's own API: http://code.google.com/apis/finance/ You can look at their finance gadgets for some example code.

    0 讨论(0)
提交回复
热议问题