Last active
August 29, 2015 14:02
-
-
Save yangsheng1107/c76ae0082cea4f58635b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #------------------------------------------------------------------------------- | |
| # Name: Yahoo Finance Stock | |
| # Purpose: | |
| # | |
| # Author: yangsheng | |
| # | |
| # Created: 04/06/2014 | |
| # Copyright: (c) yangsheng 2014 | |
| # Licence: <your licence> | |
| #------------------------------------------------------------------------------- | |
| # -*- coding: utf-8 -*- | |
| import os, csv, urllib2 | |
| # = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
| # Parameter Define | |
| # = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
| debug = 1 # debug mode, 1: enable | |
| stocks = ['2330.tw', | |
| '2498.tw', | |
| '3008.tw'] | |
| # = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
| # Function | |
| # = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
| def urlInit(url): | |
| url = "http://finance.yahoo.com/d/quotes.csv?s=" | |
| id = 0 | |
| while id < len(stocks): | |
| if id == 0: | |
| url = url + stocks[id] | |
| else: | |
| url = url + "+" + stocks[id] | |
| id += 1 | |
| url = url + "&f=snabc1p2d1t1" | |
| if debug == 1: | |
| print url | |
| return url | |
| def getStockInfo(url, filename): | |
| content = urllib2.urlopen(url).read() | |
| output = open(filename,'wb') | |
| output.write(content) | |
| output.close() | |
| return | |
| def csvParsing(filename): | |
| f = open(filename, 'r') | |
| for row in csv.DictReader(f, ["s", "n", "a", "b", "c1", "p2", "d1", "t1"]): | |
| print row['s'] + "\t " + row['a'] + "\t " + row['t1'] + "\t " + row['c1'] + "\t " + row['p2'] + "\t " + row['n'] | |
| f.close() | |
| return | |
| # = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
| # mian function | |
| # = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
| if __name__ == '__main__': | |
| filename = 'quotes.csv' | |
| url = "" | |
| # Check bin file is exist | |
| if os.path.isfile (filename): | |
| os.system ("del %s" % (filename)) | |
| url = urlInit(url) | |
| getStockInfo(url, filename) | |
| csvParsing(filename) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment