Skip to content

Instantly share code, notes, and snippets.

@yangsheng1107
Last active August 29, 2015 14:02
Show Gist options
  • Save yangsheng1107/8b22bac6d45be161db43 to your computer and use it in GitHub Desktop.
Save yangsheng1107/8b22bac6d45be161db43 to your computer and use it in GitHub Desktop.
#-------------------------------------------------------------------------------
# Name: Google Finance Stock
# Purpose:
#
# Author: yangsheng
#
# Created: 04/06/2014
# Copyright: (c) yangsheng 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
import os, urllib2
import json
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Parameter Define
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
debug = 0 # debug mode, 1: enable
stocks = ['2330',
'2498',
'3008']
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Function
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
def urlInit(url):
url = "http://www.google.com/finance/info?infotype=infoquoteall&q="
id = 0
while id < len(stocks):
if id == 0:
url = url + "TPE:" + stocks[id]
else:
url = url + ",TPE:" + stocks[id]
id += 1
if debug == 1:
print url
return url
def getStockInfo(url, filename):
content = urllib2.urlopen(url).readlines()
output = open(filename,'wb')
for lines in content:
if lines == '// [\n': # This pattern "// [" will cause json parsion error
output.write('[\n')
else:
output.write(lines)
output.close()
return
def jsonParsing(filename):
json_data = open(filename)
data = json.load(json_data)
for item in data:
print item['t'] + "\t " + item['l_cur'] + "\t " + item['ltt'] + "\t " + item['c'] + "\t " + item['cp'] + "\t " + item['name']
return
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
# mian function
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
if __name__ == '__main__':
filename = 'stock.txt'
url = ""
# Check bin file is exist
if os.path.isfile (filename):
os.system ("del %s" % (filename))
url = urlInit(url)
getStockInfo(url, filename)
jsonParsing(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment